1

我的资产文件夹中有一张图片(welcome_image.jpg)。并使用此代码从资产中读取。

Bitmap bit = null;
String url = "/assets/welcome_image.jpg";
bit = BitmapFactory.decodeFile(url);

但位为空并抛出错误

03-26 16:42:15.303: D/Image exisits....(21547): Image url : /assets/welcome_image.jpg
03-26 16:44:39.853: E/BitmapFactory(21547): Unable to decode stream: java.io.FileNotFoundException: /assets/welcome_image.jpg: open failed: ENOENT (No such file or directory)

请告诉我如何实现这一目标。

4

6 回答 6

2

您可以使用它的方法AssetManager来获取,然后使用方法来获取位图。InputStreamopen()decodeStream()BitmapFactory

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;
}
于 2013-03-26T11:24:22.027 回答
0

检查此代码

AssetManager assetManager = getAssets();
            InputStream istr;
            try {
                istr = assetManager.open("ceo.jpg");
                Bitmap bitmap = BitmapFactory.decodeStream(istr);
                imageView.setImageBitmap(bitmap);
                istr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
于 2013-03-26T11:24:30.400 回答
0

尝试如下:

 InputStream bitmap=null;
  try {
      bitmap=getAssets().open("welcome_image.jpg");
     Bitmap bit=BitmapFactory.decodeStream(bitmap);
    img.setImageBitmap(bit);
  } catch (IOException e) {
      e.printStackTrace();
} finally {
   if(bitmap!=null)
    bitmap.close();
  }
于 2013-03-26T11:24:38.603 回答
0

尝试这个:

   InputStream bitmap=null;

  try {
bitmap=getAssets().open("welcome_image.jpg");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
img.setImageBitmap(bit);
} catch (IOException e) {
e.printStackTrace();
 } finally {
if(bitmap!=null)
bitmap.close();
  }
于 2013-03-26T11:24:54.497 回答
0

您应该使用以下路径:

Bitmap bit = null;
bit = BitmapFactory.decodeFile("file:///android_asset/welcome_image.jpg");
于 2013-03-26T11:23:06.083 回答
0
AssetManager assetManager = getAssets();

    InputStream istr = assetManager.open(fileName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
于 2013-03-26T11:26:36.863 回答