概述
问题诉求:
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 byAssetManager.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目录下,然后把文件名传到接口函数
最后
以上就是勤恳飞鸟为你收集整理的JNI怎样访问Asset 目录下文件Asset 不是普通的文件,是打包、压缩到APK的解决方法:APP相关的cache 和files最终解决方案:的全部内容,希望文章能够帮你解决JNI怎样访问Asset 目录下文件Asset 不是普通的文件,是打包、压缩到APK的解决方法:APP相关的cache 和files最终解决方案:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复