将assets文件转为byte[]

注意:assets文件在apk安装到手机后,在手机里没有绝对路径,只有相对路径!!!

代码:

animRes.m_AnimSklFile = readFileFromAssets(this.getActivity(),null,"animation.skl");
animRes.m_AnimFiles[0] = readFileFromAssets(this.getActivity(),null,"animation.anim");
......
public static byte[] readFileFromAssets(Context context, String groupPath, String filename){
    byte[] buffer = null;
    AssetManager am = context.getAssets();
    try {
        InputStream inputStream = null;
        if (groupPath != null) {
            inputStream = am.open(groupPath + "/" + filename);
        } else {
            inputStream = am.open(filename);
        }

        int length = inputStream.available();
        ArcLog.d(TAG, "readFileFromAssets length:" + length);
        buffer = new byte[length];
        inputStream.read(buffer);
    }catch (Exception exception){
        exception.printStackTrace();
    }
    return buffer;
}

猜你喜欢

转载自blog.csdn.net/u012906122/article/details/103653611