栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JNI怎样访问Asset 目录下文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JNI怎样访问Asset 目录下文件

问题诉求:

native 库函数的输入参数是个文件的路径,如果把文件放到APK的 asset路径下,该怎样得到?

Asset 不是普通的文件,是打包、压缩到APK的

There is no "absolute path for a file existing in the asset folder". The content of your project's assets/ folder are packaged in the APK file. Use an AssetManager object to get an InputStream on an asset. 

Assets are compiled into .apk file (which basically is zip file). You can't get system path into zip file. You have to manually unzip .apk to some folder and get file system path to asset folder and files inside that folder.,

解决方法:
  • 1] you could extract the assets into some directory but this will take extra space

       在java 层使用asset manager extract 文件到另外的路径如/data/data/your app/cache等路径   
Question How to set file path in Android JNI?https://www.titanwolf.org/Network/q/1cb46381-72c9-4749-8456-462ac4983650/y

  • 2] you could (bunlde and) use something like libzip to read the assets from the APK in   C.

        引入libzip把asset文件从APK中解压出

  • 3] or, to avoid bundling an extra library, my personal preference is to read data in C, using JNI, from the Java InputStream object returned by AssetManager.open(). It takes a little code but it works great.

       不引入另外的 库,也不把asset文件解压到另外的路径,直接在JNI中读

Android: 在native中访问assets全解析 - willhua - 博客园本文总结在Android Native C++开发中访问APK中的 assets 资源的方法 在CMake中添加相关NDK LIB的 依赖 因为我们接下来用到的一些函数实现在NDK库libandroihttps://www.cnblogs.com/willhua/p/9692529.html

APP相关的cache 和files

Asset文件 解压到哪里?

Cache

This is an app-specific directory on the filesystem. The intent for this directory is store temporary data your application may need to keep around between sessions, but may not be vital to keep them forever. You typically access this directory with Context.getCacheDir(). This will show up as "Cache" on your app settings.

Files

Like the cache directory, your app also has an app-specific directory for holding files. Files in this directory will exist until the app explicitly deletes them or the app is uninstalled. You typically access this directory with Context.getFilesDir(). This can show up as various things on the app info screen, but in your screenshot this is "USB Storage Data".

NOTE: If you want to explicitly place on external media (typically SD card), you can use Context.getExternalFilesDir(String type).

最终解决方案:

上面的三种解决方案,看上去3 最好,但不满足函数接口的定义(参数是个文件路径名), 最终选择方案1,extract asset 文件到 data/data/you app/cache目录下,然后把文件名传到接口函数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号