assets文件转为byte[]
程序员文章站
2022-07-14 21:54:54
...
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;
}