AssetManager中的路徑參數不能包含"assets/"


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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM