我需要从 Assets 中获取位图和声音。我尝试这样做:
BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");
像这样:
getBitmapFromAsset("Files/Numbers/l1.png");
private Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
但我得到的只是可用空间,而不是图像。
如何做到这一点?
最佳答案
public static Bitmap getBitmapFromAsset(Context context, String filePath) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(filePath);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// handle exception
}
return bitmap;
}
路径就是您的文件名 fx bitmap.png。如果你使用子文件夹 bitmap/那么它的 bitmap/bitmap.png
关于android从 Assets 中获取位图或声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501309/