微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android使用C(ndk)从资产文件夹读取文本文件

我需要从 android中的资产文件夹中读取文本文件,通过互联网进行搜索,我发现有来自android 2.3以上的asset_manager api.由于我只针对平板电脑设备,这是非常有用的.但是,由于我不是C语言的专家,我无法找到任何关于如何使用文件描述符读/写文件的例子.我发现许多使用FILE *的例子(文件指针)

My goal is to decrypt a js file from asset folder which is encrypted
using C (for securing the code),as js code is visible if end user
decompiled my apk. Because asset folder is inside zip file is it possible to do?

解决方法

这是使用asset_manager ndk lib从android资产文件夹中读取文件代码
AAssetManager* mgr = AAssetManager_fromJava(env,assetManager);
    AAsset* asset = AAssetManager_open(mgr,(const char *) js,AASSET_MODE_UNKNowN);
    if (NULL == asset) {
        __android_log_print(ANDROID_LOG_ERROR,NF_LOG_TAG,"_ASSET_NOT_FOUND_");
        return JNI_FALSE;
    }
    long size = AAsset_getLength(asset);
    char* buffer = (char*) malloc (sizeof(char)*size);
    AAsset_read (asset,buffer,size);
    __android_log_print(ANDROID_LOG_ERROR,buffer);
    AAsset_close(asset);

添加以下行到我的Android.mk

# for native asset manager
LOCAL_LDLIBS    += -landroid

并且不要忘记源文件中的include

#include <android/asset_manager.h>

原文地址:https://www.jb51.cc/android/311058.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐