【问题标题】:Android: FileNotFoundException on an image in drawableAndroid:可绘制图像上的 FileNotFoundException
【发布时间】:2013-04-06 06:44:03
【问题描述】:

我正在尝试使用我在整个 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:drawable/filename”。我肯定有一个名称为我试图加载到可绘制文件夹中的文件。有任何想法吗?我尝试过使用和不使用文件扩展名,尝试将文件放入每个文件夹并尝试多个手机/模拟器。

【问题讨论】:

  • 尝试新的 BufferedInputStream((assets.open(.getResources().getDrawable(R.drawble.img)));
  • 您的图片名称是否包含大写字母?

标签: android android-drawable


【解决方案1】:

AssetManager 旨在访问assets 文件夹中的数据。因此,在您的示例中,它查找 assets/drawable/filename 并没有找到任何内容。

要从drawable中获取资源,应该使用

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

如果你确定它是位图,你可以这样做:

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

【讨论】:

    【解决方案2】:

    你可以这样...

    // 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);
    

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多