安卓:FileNotFoundException异常在绘制的图像 [英] Android: FileNotFoundException on an image in drawable

查看:236
本文介绍了安卓:FileNotFoundException异常在绘制的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个code,我已经看到了多个职位遍及计算器

I'm trying to use this code that I've seen on multiple posts throughout stackoverflow

    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异常:可绘制/文件名。我肯定有我想要的绘制文件夹来加载名称的文件。有任何想法吗?我试着和没有文件扩展名,试图把文件放到每个文件夹,并试图多部电话/仿真器。

but I'm getting a "FileNotFoundException: drawable/filename". I definitely have a file with the name I'm trying to load in the drawable folders. Any ideas? I've tried with and without the file extension, tried putting the file into every folder and tried multiple phones/emulator.

推荐答案

AssetManager 打算访问在资产数据文件夹中。因此,在你的榜样,它看起来资产/绘制/文件名并没有发现任何东西。

The AssetManager is intended to access the data in the assets folder. So in your example, it looks for assets/drawable/filename and does not find anything.

要从绘制获取资源,应该使用

To get a resource from drawable, one should use

Drawable d = getResources().getDrawable(R.drawable.filename);

如果你确定这是一个位图,你可以做像这样:

If you're sure it's a bitmap, you can do it like so:

Bitmap bm = ((BitmapDrawable)getResources()
                .getDrawable(R.drawable.filename)).getBitmap();

这篇关于安卓:FileNotFoundException异常在绘制的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆