JNI获取assets文件夹内的文件地址

0、前言

在深度学习的JNI时,需要把模型文件放到assets里,加载时,却不知道模型的目录地址,报错:该文件未找到.

因为assert文件夹 只是Android系统管理,加载模型需要从系统文件中读取.所以需要把assert的文件写到系统文件中.

1、NCNN

ncnn已经集成了AAssetManager,在模型加载时,传入AAssetManager 参数,他就会实现自动写入系统,并读取相应目录地址,JNI例子如下:

#include <jni.h>
#include <string>
#include <iostream>
#include <net.h>
#include <android/asset_manager_jni.h>   //AAssetManager需要
​
​
//模型文件mnet.25-opt.param、mnet.25-opt.bin放在asserts中
static ncnn::Net retinaface;
extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_retainfacencnn_RetinaFace_Init(JNIEnv *env, jobject thiz, jobject assetManager) {
    AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
    int ret = retinaface.load_param(mgr, "mnet.25-opt.param");
    ret = retinaface.load_model(mgr, "mnet.25-opt.bin");

AAssetManager其他功能:

AAssetManager *mgr = AAssetManager_fromJava(env, assetManager)
//打开assert文件夹
AAssetDir *dir = AAssetManager_openDir(mgr,"");
//打开assert下的子文件夹part1
AAssetDir *dir = AAssetManager_openDir(mgr,"part1");
//获取文件名
const char *file = nullptr;
file =AAssetDir_getNextFileName(dir));
//打开文件
AAsset* asset = AAssetManager_open(AAssetManager* mgr, const char* filename, int mode);
		//mode 可以为AASSET_MODE_STREAMING
//获取文件长度 获取文件长度,如果文件不是很大可以直接用malloc分配空间使用AAsset_read进度文件内容读取。
int len = AAsset_getLength(asset);
//读取文件  
unsigned char *buf = (unsigned char *) malloc(sizeof(unsigned char) * len);
AAsset_read(asset, buf, static_cast<size_t>(len));
//关闭文件
AAsset_close(asset);
//关闭文件夹
AAssetDir_close(dir);

2、手动获取地址

CPP无法直接获取文件目录地址,需要在JAVA中对文件进行拷贝到系统中,获取它的绝对地址,传给JNI

2.1 JAVA类方法

2.1.1 方法1

package com.ylz.seetaface;
import java.io.FileNotFoundException;   //异常
import android.content.res.AssetManager;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String cstapath = getPath("face_detector.csta", this);    //调用获取文件地址方法
         	boolean ret_init = seetaFace.loadModel(cstapath)  //加载模型
        ...
        (其他代码略)
        ...
    //获取文件地址方法
    private static String getPath(String file, Context context) {
            AssetManager assetManager = context.getAssets();
            BufferedInputStream inputStream = null;
            try {
                // Read data from assets.
                inputStream = new BufferedInputStream(assetManager.open(file)); //打开文件放入输入流中
                byte[] data = new byte[inputStream.available()]; //输入流信息放入data中
                inputStream.read(data);  //输入流读取data
                inputStream.close();  //输入流关闭
                // Create copy file in storage.
                File outFile = new File(context.getFilesDir(), file);  //新文件(夹)创建,应用文件目录
                FileOutputStream os = new FileOutputStream(outFile);  //输出流
                os.write(data);  //写入数据.即完成了拷贝
                os.close();  //关闭
                // Return a path to file which may be read in common way.
                Log.d("filePath:",outFile.getAbsolutePath());
                return outFile.getAbsolutePath();   //得到新文件的绝对地址
            } catch (IOException ex) {
                Log.i("打开失败", "无法打开此文件");
            }
            return "";
        }
    }
}

2.1.2 方法2

package com.ylz.seetaface;
import java.io.FileNotFoundException;   //异常
import android.content.res.AssetManager;   //获取getAssets()
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.File;   //获取getCacheDir()
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            boolean mkdir_model = copyAssetAndWrite("face_detector.csta");
            if(mkdir_model==true){
                File dataFile=new File(getCacheDir(),"face_detector.csta");
                String cstapath=dataFile.getAbsolutePath();
                Log.e("文件地址为:", cstapath);
                boolean ret_init = seetaFace.loadModel(cstapath);
            }
        ...
        (其他代码略)
        ...
    //获取文件地址方法    
    private boolean copyAssetAndWrite(String fileName){
        try {
            File cacheDir=getCacheDir();  //获取文件夹   缓冲目录
            if (!cacheDir.exists()){
                cacheDir.mkdirs();
            }
            File outFile =new File(cacheDir,fileName);   //新建文件
            if (!outFile.exists()){
                boolean res=outFile.createNewFile();  //如果不存在,则新建
                if (!res){
                    return false;  //新建失败
                }
            }else {
                if (outFile.length()>10){//表示已经写入一次
                    return true;
                }
            }
            InputStream is=getAssets().open(fileName);   //输入流打开文件
            FileOutputStream fos = new FileOutputStream(outFile);  //新文件(这时候还是空)放入输出流
            byte[] buffer = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);  //输出流写入数据
            }
            fos.flush();
            is.close();  //输入流关闭
            fos.close();  //输出流关闭
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

2.2 JNI中方法

#include <jni.h>
#include <string>
#include <seeta/Common/Struct.h>

static Seetaface seetaNet;

extern "C" JNIEXPORT jboolean
Java_com_ylz_seetaface_SeetaFace_loadModel(JNIEnv *env,jobject thiz,jstring cstapath) {
    //模型初始化
    const char *modelpath = env->GetStringUTFChars(cstapath, 0); //jstring转char*
    LOGI("model_path:%s",modelpath);
    seetaNet.Init(model_path);
    env->ReleaseStringUTFChars(cstapath, modelpath);

2.3 获取的地址

两种方法获取的地址区别:

方法一获取的文件地址为:/data/user/0/com.ylz.seetaface/files/face_detector.csta

  • com.ylz.seetaface : 应用创建的JAVA类名

  • files: 利用getFilesDir(),得到的是应用文件目录

方法二获取的文件地址为:/data/user/0/com.ylz.seetaface/cache/face_detector.csta

  • cache: 利用getCacheDir() 得到的是应用缓存目录

区别在于子文件夹名,方法1为files, 方法2为cache

只需修改代码中的getFilesDir()或者getCacheDir() 即可保持一致

各自获取子文件夹目录地址转string:

getCacheDir().getPath();
getFilesDir().getPath();

参考1(Android JNI实现图片处理和coco-mobilenet模型加载(OpenCV) - 简书) Android JNI实现图片处理和coco-mobilenet模型加载(OpenCV)

参考2(Android获取assets文件路径_dreamsever的专栏-CSDN博客_android获取assets文件路径) Android获取assets文件路径

参考3([Android]JNI进阶1-读取本地文件夹 - 简书) [Android]JNI进阶1-读取本地文件夹

[参考4] (10条消息) Android存储路径获取,getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的区别_e电动小马达e的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值