SO Android 库的运行时目录在哪里? [英] Where is the runtime directory for SO Android libs?

查看:57
本文介绍了SO Android 库的运行时目录在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Android 应用,它使用外部 .so 库来工作 (OpenALPR).

I have an Android app that uses an external .so library to work (OpenALPR).

这个 .so 库还需要一个外部 conf 文件才能正常工作.当我加载我的库并初始化它时,我需要在本机函数中指定 conf 文件到库的路径.

This .so library also needs an external conf file to work properly. When I load my library and initialize it, I need to specify the path of the conf file to the library, in a native function.

private native void initialize(String country, String configFile, String runtimeDir);

这是我的项目结构:

我应该给哪条路?我不知道把我的文件放在哪里,以便我的图书馆可以看到它们

推荐答案

诀窍是手动将 Assets 的内容移动到实际的 /data/data/com.example.app/ 文件夹中,这是存储库的地方.

The trick was to move manually the content of Assets to the actual /data/data/com.example.app/ folder, which is where the libs are stored.

这是实现这一目标的片段(来自官方 android 存储库)

Here's a snippet that achieves that (from the official 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);
        }
    }

这篇关于SO Android 库的运行时目录在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆