2013-03-26 121 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);