Android load library error -- failed to map segment
I am loading a native library using JNI. The library resides on the sdcard.
I am loading the library with this code:
try
{
File str;
String storage;
str = Environment.getExternalStorageDirectory();
storage = str.getAbsolutePath();
File f = new File(storage+"/Android/data/org.ilri.IMPACTLight/libIMPInterface.so");
if (f.exists())
System.load(storage+"/Android/data/org.ilri.IMPACTLight/libIMPI开发者_如何转开发nterface.so");
}
catch (SecurityException e)
{
Log.i(QtTAG, "Security: Can't load /mnt/sdcard/Android/data/org.ilri.IMPACTLight/libIMPInterface.so", e);
}
catch (UnsatisfiedLinkError e)
{
Log.i(QtTAG, "Link: Can't load /mnt/sdcard/Android/data/org.ilri.IMPACTLight/libIMPInterface.so", e);
}
However I always get:
D/dalvikvm( 408): Trying to load lib /mnt/sdcard/Android/data/org.ilri.GPSTest/libGPSInterface.so 0x44ede2c0 I/dalvikvm( 408): Unable to dlopen(/mnt/sdcard/Android/data/org.ilri.GPSTest/libGPSInterface.so): Cannot load library: load_segments[907]: 33 failed to map segment from 'libGPSInterface.so' @ 0x81900000 (0x00003470). p_vaddr=0x00000000 p_offset=0x00000000
I get this on a AVD 2.2
Any idea how to solve this is much appreciated!!!
Thanks, Carlos
You cannot load native libraries or execute binaries located on the SDCard. It is mounted noexec.
You need to copy the library to the internal storage before you load it, that is, in a subdirectory of dataDir
, eg:
PackageManager pm = context.getPackageManager();
String dataDir = pm.getApplicationInfo(context.getPackageName(), 0).dataDir;
// Create a directory like dataDir/mylibs/ and copy the library in it
Add your .so file inside your application and use the following..
System.loadLibrary("libIMPInterface.so");
精彩评论