我需要得到位图,并从资产声音。我试着这样做:
BitmapFactory.de codeFILE(文件:///android_asset/Files/Numbers/l1.png);
和这样的:
getBitmapFromAsset(文件/号/ l1.png);
私人位图getBitmapFromAsset(字符串则strName){
AssetManager assetManager = getAssets();
InputStream的ISTR = NULL;
尝试 {
ISTR = assetManager.open(则strName);
}赶上(IOException异常E){
e.printStackTrace();
}
点阵位图= BitmapFactory.de codeStream(ISTR);
返回的位图;
}
不过,我得到的只是自由空间,没有图像。
如何做到这一点?
解决方案 公共静态位图getBitmapFromAsset(上下文的背景下,字符串文件路径){
AssetManager assetManager = context.getAssets();
InputStream的ISTR;
点阵位图= NULL;
尝试 {
ISTR = assetManager.open(文件路径);
位= BitmapFactory.de codeStream(ISTR);
}赶上(IOException异常E){
//处理异常
}
返回的位图;
}
的路径就是你的文件名FX bitmap.png。如果你使用的子文件夹中的位图/那么它的位图/ bitmap.png
I need to get Bitmap and sound from assets. I try to do like this:
BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");
And like this:
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;
}
But I get just free space, not image.
How to do this?
解决方案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;
}
the path is simply your file name fx bitmap.png. if you use subfolder bitmap/ then its bitmap/bitmap.png