android - SO Android 库的运行时目录在哪里?
问题描述
我有一个使用外部.so
库工作的 Android 应用程序(OpenALPR)。
该.so
库还需要一个外部 conf 文件才能正常工作。当我加载我的库并初始化它时,我需要在本机函数中指定 conf 文件到库的路径。
private native void initialize(String country, String configFile, String runtimeDir);
这是我的项目的结构:
我应该给哪条路?我找不到将文件放在哪里以便我的库可以看到它们
解决方案
诀窍是手动将 Assets 的内容移动到/data/data/com.example.app/
存储库的实际文件夹中。
这是一个实现这一点的片段(来自官方的android repo)
/**
* Copies the assets folder.
*
* @param assetManager The assets manager.
* @param fromAssetPath The from assets path.
* @param toPath The to assets path.
*
* @return A boolean indicating if the process went as expected.
*/
public static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
try {
String[] files = assetManager.list(fromAssetPath);
new File(toPath).mkdirs();
boolean res = true;
for (String file : files)
if (file.contains(".")) {
res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
} else {
res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
}
return res;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Copies an asset to the application folder.
*
* @param assetManager The asset manager.
* @param fromAssetPath The from assets path.
* @param toPath The to assests path.
*
* @return A boolean indicating if the process went as expected.
*/
private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Copies a file.
*
* @param in The input stream.
* @param out The output stream.
*
* @throws IOException
*/
private static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
推荐阅读
- ssl - strong-soap:通过 https 调用服务方法
- c# - EntityType 'DbGeography' 没有在 EF6 上定义的键
- java - 无法在 macOS Catalina 上动态设置 JAVA_HOME
- google-sheets - 需要“大于”的表行中 SUM IF 的数组公式
- wordpress - 如何在 Woocommerce 中为私人产品显示/允许“添加到购物车”
- python - 要从另一个列表中找到的列表中删除一个值?
- xamarin - 如何使用 Xamarin.Forms - AppShell 在导航栏中隐藏后退按钮?
- google-cloud-platform - GCP“12 个月 300 美元免费试用”因结算帐户更改而被删除?
- reactjs - 为什么更新状态后子组件渲染在父div之外?
- rust - rustc 关于价值的误报警告从未读过?