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, , NULL);
struct zip_stat fstat;
struct zip_file* file = zip_fopen(apkArchive, fileName, ); if (!file) {
loge("Error opening %s from APK", fileName);
return -;
} zip_stat(apkArchive,fileName,,&fstat); off_t bfsize = fstat.size;
*buffer=(char *)malloc(bfsize+);
memset(*buffer, 0x0, bfsize+);
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+);
memset(buffer, , bufferSize);
buffer[bufferSize]=;
int numBytesRead = AAsset_read(asset, buffer, bufferSize);
AAsset_close(asset);

最新文章

  1. set、def、lambda、内置函数、文件操作
  2. htnl5中设置文本单行显示,超出部分打省略号,鼠标移到文本时alt出全部文本内容
  3. nginx 引号 x22
  4. Unity NGUI 资源下载
  5. DBAccess
  6. 用自己的ID在appstore中更新app-黑苹果之路
  7. csharp: MVC Controls
  8. hive 传递变量的两种方式
  9. 与后台进行连接,mysql模块 第六篇
  10. poj 1018 Communication System 枚举 VS 贪心
  11. 转 excel表怎么自动分列
  12. 使用Groovy处理SoapUI中Json response
  13. 当final作用于变量、参数、方法和类时该如何处理
  14. 类 Random
  15. 利用Audacity软件分析ctf音频隐写
  16. [转] ES6 import/export:模块导入导出方式
  17. HDU 5583 Kingdom of Black and White(暴力)
  18. Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)
  19. Oracle与SQLSERVER修改数据文件的路径
  20. Spring ApplicationListener 理解

热门文章

  1. GridView.GridLines 属性
  2. android入门——Activity(2)
  3. 错误提示:在此上下文中不允许使用名称 "***"。有效表达式包括常量、 常量表达式和变量(在某些上下文中),不允许使用列名。
  4. 存储过程获取新插入记录ID
  5. Lavarel(-) windows 部署
  6. Codeigniter使用phpexcel
  7. this .运算符 和 [] 运算符
  8. Qt 如何处理密集型耗时的事情(频繁调用QApplication::processEvents)
  9. 工具类_java 数字转化为汉字大写
  10. smarty如何处理状态值的显示