AndroidJNI拷贝assets文件到私有目录下

public class AssetCopyHelper {
    static {
        System.loadLibrary("asset_copy");
    }
 
    public native void copyAssetToDirectory(String assetName, String destPath);
}

c++实现:

#include <jni.h>
#include <android/asset_manager.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
 
#define LOG_TAG "AssetCopyHelper"
#define LOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
#define LOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
 
JNIEXPORT void JNICALL
Java_com_example_yourapp_AssetCopyHelper_copyAssetToDirectory(JNIEnv *env, jobject obj, jstring assetName, jstring destPath) {
    AAssetManager* assetManager = AAssetManager_fromJava(env, assetManagerObj);
    const char* nativeAssetName = env->GetStringUTFChars(assetName, 0);
    const char* nativeDestPath = env->GetStringUTFChars(destPath, 0);
 
    AAsset* asset = AAssetManager_open(assetManager, nativeAssetName, AASSET_MODE_BUFFER);
    if (asset == NULL) {
        LOGE("Could not open asset %s", nativeAssetName);
        return;
    }
 
    off_t assetLength = AAsset_getLength(asset);
    void* assetBuffer = malloc(assetLength);
    AAsset_read(asset, assetBuffer, assetLength);
 
    FILE* destFile = fopen(nativeDestPath, "wb");
    if (destFile == NULL) {
        LOGE("Could not open file %s", nativeDestPath);
        free(assetBuffer);
        AAsset_close(asset);
        return;
    }
 
    fwrite(assetBuffer, 1, assetLength, destFile);
    fclose(destFile);
 
    free(assetBuffer);
    AAsset_close(asset);
 
    env->ReleaseStringUTFChars(assetName, nativeAssetName);
    env->ReleaseStringUTFChars(destPath, nativeDestPath);
}

c实现:

JNIEXPORT void JNICALL
Java_com_example_yourapp_AssetCopyHelper_copyAssetToDirectory(JNIEnv *env, jobject obj, jstring assetName, jstring destPath) { {

    const char* nativeAssetName = (*env)->GetStringUTFChars(env,assetName, 0);
    const char* nativeDestPath = (*env)->GetStringUTFChars(env,destPath, 0);

    AAssetManager* asMg = AAssetManager_fromJava(env, assetManager);
    AAsset* asset = AAssetManager_open(asMg,assetName,AASSET_MODE_UNKNOWN   );

    if(asset==NULL)
    {
        LOGI("%s not found in asset",assetName);
        return;
    }
    FILE* destFile = fopen(nativeDestPath, "wb");

    int assetLength = AAsset_getLength(asset);

    char *assetBuffer = (char *)malloc(assetLength);
    memset(assetBuffer, 0x00, assetLength);
    int numBytesRead = AAsset_read(asset, assetBuffer, assetLength);

    FILE* destFile = fopen(dexPath, "wb");
    if (destFile == NULL) {
        LOGE("Could not open file %s", nativeDestPath);
        free(assetBuffer);
        AAsset_close(asset);
        return;
    }

    fwrite(assetBuffer, 1, assetLength, destFile);
    fclose(destFile);

    free(assetBuffer);
    AAsset_close(asset);

    (*env)->ReleaseStringUTFChars(env,assetName, nativeAssetName);
    (*env)->ReleaseStringUTFChars(env,destPath, nativeDestPath);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值