Android:FileNotFoundException在drawable中的图像上
我正在尝试使用我在stackoverflow的多篇文章中看到的代码Android:FileNotFoundException在drawable中的图像上,android,android-drawable,Android,Android Drawable,我正在尝试使用我在stackoverflow的多篇文章中看到的代码 public static Bitmap loadBitmap(Context context, String filename) throws IOException { AssetManager assets = context.getResources().getAssets(); InputStream buf = new BufferedInputStream((as
public static Bitmap loadBitmap(Context context, String filename) throws IOException {
AssetManager assets = context.getResources().getAssets();
InputStream buf = new BufferedInputStream((assets.open("drawable/" + filename)));
Bitmap bitmap = BitmapFactory.decodeStream(buf);
return bitmap;
}
但我得到了一个FileNotFoundException:drawable/filename。我肯定有一个文件的名称,我正试图加载到绘图文件夹。有什么想法吗?我尝试过使用和不使用文件扩展名,尝试过将文件放入每个文件夹,并尝试了多个Phone/emulator。AssetManager旨在访问assets文件夹中的数据。因此,在您的示例中,它查找assets/drawable/filename,但没有找到任何内容
要从drawable获取资源,应该使用
Drawable d = getResources().getDrawable(R.drawable.filename);
如果您确定它是位图,您可以这样做:
Bitmap bm = ((BitmapDrawable)getResources()
.getDrawable(R.drawable.filename)).getBitmap();
你可以这样做
// load image from Assets folder
// get input stream
InputStream mIms = getAssets().open("ic_launcher.png");
// load image as Drawable
Drawable mDraw = Drawable.createFromStream(mIms, null);
// set image to ImageView
mIms.setImageDrawable(mDraw);
尝试新建BufferedInputStreamassets.open.getResources.getDrawableR.drawble.img;您的图像名称是否包含大写字母?