2013-03-26 128 views
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) 

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

+0

试试这个 InputStream ims = getAssets()。open(“welcome_image.jpg”); //载入图像为可绘制 Drawable d = Drawable.createFromStream(ims,null); – 2013-03-26 11:24:43

回答

0

应使用以下路径:

Bitmap bit = null; 
bit = BitmapFactory.decodeFile("file:///android_asset/welcome_image.jpg"); 
+0

我尝试过,但得到的答复为空 – vignesh 2013-03-26 11:34:01

1

您可以使用AssetManager使用其open()方法获取InputStream然后使用BitmapFactorydecodeStream()方法来获取位图。

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; 
} 
+0

这绝对有效。 – 2013-08-04 19:58:43

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(); 
      } 
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(); 
    } 
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(); 
    } 
0
AssetManager assetManager = getAssets(); 

    InputStream istr = assetManager.open(fileName); 
    Bitmap bitmap = BitmapFactory.decodeStream(istr); 
相关问题