zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4831465.html
Copyright © 2011-2022 走看看