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>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...