String path = “music/bg.mp3”; //正确的参数 //String path = “assets/music/bg.mp3”; //错误的参数 //String path = “file:///android_asset/music/bg.mp3”; //错误的参数 //String path = “/music/bg.mp3”; //错误的参数 AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); assetFileDescritor.close();
1. java代码中AssetManager的路径参数不能包含"assets/",
2. 但是在NDK编程中C代码里面如果采用zip方式访问assets的文件,必须包含"assets/"
//ReadAssets fileName = assets/rabbit/img/banana_skin.png off_t readFileFromAsset(const char* fileName, char ** buffer) { logd("ReadAssets fileName = %s", fileNmae); struct zip* apkArchive=zip_open(assetPath, 0, NULL); struct zip_stat fstat; struct zip_file* file = zip_fopen(apkArchive, fileName, 0); if (!file) { loge("Error opening %s from APK", fileName); return -1; } zip_stat(apkArchive,fileName,0,&fstat); off_t bfsize = fstat.size; *buffer=(char *)malloc(bfsize+1); memset(*buffer, 0x0, bfsize+1); int numBytesRead = zip_fread(file, *buffer,bfsize);; zip_fclose(file); return bfsize; }
上述代码中assetPath是在java层传过来的,通过调用String assetPath = context.getPackageResourcePath(),传递到c++层调用
const char* assetPath = env->GetStringUTFChars(jassetPath, false);
env->ReleaseStringUTFChars(jassetPath, assetPath);
得到。
3. 在C++层使用AAssetManager也是一样必须以"assets/"开头。
AAsset* asset = AAssetManager_open(assetMgr, realPath, AASSET_MODE_UNKNOWN); off_t bufferSize = AAsset_getLength(asset); char* buffer=(char*)malloc(bufferSize+1); memset(buffer, 0, bufferSize); buffer[bufferSize]=0; int numBytesRead = AAsset_read(asset, buffer, bufferSize); AAsset_close(asset);