導航:首頁 > 文件教程 > android載入so文件

android載入so文件

發布時間:2025-09-25 00:17:42

㈠ 如何載入so文件 android

android中載入so文件:

在Android中調用動態庫文件(*.so)都是通過jni的方式,而且往往在apk或jar包中調用so文件時,都要將對應so文件打包進apk或jar包,工程目錄下圖:

Android中載入so文件的提供的API:

void System.load(String pathName);

說明:

1、pathName:文件名+文件路勁;

2、該方法調用成功後so文件中的導出函數都將插入的系統提供的一個映射表(類型Map);

3、具體代碼如下:

try {
String localPath = Environment.getExternalStorageDirectory() + path;
Log.v(TAG, "LazyBandingLib localPath:" + localPath);

String[] tokens = mPatterns.split(path);
if (null == tokens || tokens.length <= 0
|| tokens[tokens.length - 1] == "") {
Log.v(TAG, "非法的文件路徑!");
return -3;
}
// 開辟一個輸入流
File inFile = new File(localPath);
// 判斷需載入的文件是否存在
if (!inFile.exists()) {
// 下載遠程驅動文件
Log.v(TAG, inFile.getAbsolutePath() + " is not fond!");
return 1;
}
FileInputStream fis = new FileInputStream(inFile);

File dir = context.getDir("libs", Context.MODE_PRIVATE);
// 獲取驅動文件輸出流
File soFile = new File(dir, tokens[tokens.length - 1]);
if (!soFile.exists()) {
Log.v(TAG, "### " + soFile.getAbsolutePath() + " is not exists");
FileOutputStream fos = new FileOutputStream(soFile);
Log.v(TAG, "FileOutputStream:" + fos.toString() + ",tokens:"
+ tokens[tokens.length - 1]);

// 位元組數組輸出流,寫入到內存中(ram)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
// 從內存到寫入到具體文件
fos.write(baos.toByteArray());
// 關閉文件流
baos.close();
fos.close();
}
fis.close();
Log.v(TAG, "### System.load start");
// 載入外設驅動
System.load(soFile.getAbsolutePath());
Log.v(TAG, "### System.load End");

return 0;

} catch (Exception e) {
Log.v(TAG, "Exception " + e.getMessage());
e.printStackTrace();
return -1;

}

閱讀全文

與android載入so文件相關的資料

熱點內容
二手手機在哪個網站買可靠 瀏覽:667
如何把文件框架變大 瀏覽:590
兩資料庫同步 瀏覽:348
怎麼做小型資料庫 瀏覽:350
數控g02圓弧怎麼編程 瀏覽:764
計算機二級c程序設計 瀏覽:362
如何升級谷歌play服務 瀏覽:805
86版本光之助力傷害計算 瀏覽:959
蘋果微電影優酷視頻 瀏覽:2
qq游戲寶寶升級 瀏覽:739
武俠類微信公眾號 瀏覽:815
matlab工具包pls 瀏覽:550
js控制滾動條到最底部 瀏覽:900
網路管理員歷年真題 瀏覽:880
編程語言適合學什麼 瀏覽:362
天正怎麼導cad文件 瀏覽:842
下載蘋果6手機壁紙 瀏覽:77
怎麼部分壓縮文件 瀏覽:80
php源碼安裝到空間教程 瀏覽:680
航天金稅注冊文件 瀏覽:88

友情鏈接