2014-03-31 28 views
0

我的資產文件夾中有一個.png文件,我試圖在點擊屏幕時將該圖像顯示在屏幕上。爲什麼我的代碼中出現「資產未找到」錯誤?

InputStream open = null; 
try{ 
    open = asset.open("ic_launcher.png"); 
    Bitmap bitmap = BitmapFactory.decodeStream(open); 

    ImageView image = (ImageView)findViewById(R.id.imageView1); 
    image.setImageBitmap(bitmap); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 
finally{ 
    if(open != null){ 
     try{ 
      open.close(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     }  
    } 
} 

但我一直得到FileNotFoundException,不知道爲什麼。

+1

到底在哪'ic_launcher.png'放? – gunar

+0

我的項目的'assets'文件夾。 –

+0

is ic_launcher.png存儲在assets.put中您的完整代碼。初始化資產 –

回答

0

檢查以下代碼行並將其替換。

InputStream is = getAssets().open("ic_launcher.png"); 
0

使用,

open =getAssets().open("icon.png"); 
+1

它爲什麼應該有所作爲? – Blackbelt

+0

我也試過,同樣的錯誤。 –

0

試試這個,它可能工作。

open = asset.open 
("ic_launcher"); 

不提及延伸。

+0

這沒有奏效。但是,我清理了該項目並重新啓動了它。現在我得到一個不同的錯誤。我得到一個NPE錯誤.. –

+2

忘記了可繪製和使用資產?您存儲在資產中的圖像不會被壓縮。最好使用drawable。 –

0

嘗試這個 -

try { 
InputStream ims = getAssets().open("ic_launcher.png"); 
Drawable d = Drawable.createFromStream(ims, null); 
imgView.setImageDrawable(d); 
} 
catch(IOException ex) { 
return; 
} 

而且check this.

相關問題