2012-06-01 79 views
2

我想用MyClass中的對象填充ListView項目。該課程的一個屬性是jpg圖像。我把我的圖像放在圖像/文件夾中。我使用此代碼用於填充在ImageView中看不到圖像android

private static final String ASSETS_DIR = "images/"; 
String imgFilePath=ASSETS_DIR+r.resourceID; 

    try{ 
    Bitmap bitmap = BitmapFactory.decodeFile(imgFilePath); 
    resourceIdView.setImageBitmap(bitmap); 


    } 
    catch(Exception e) 
    { 
     System.out.println(" Error"); 

    } 

r.resourceID是圖像的名稱,例如「AUD.jpg」 resourceIDView是ImageView的 程序並不在catch部分得到,但我不能看到圖像 有人可以幫助我?

回答

1

根據您的命名約定,我得出結論,您正在將圖像存儲在「資產」文件夾中。如果是的話,那麼你可以使用下面的代碼行,並解決這個問題:

private static final String ASSETS_DIR = "images/"; 
String imgFilePath=ASSETS_DIR+r.resourceID; 

try{ 
    Drawable d = Drawable.createFromStream(getAssets().open(imgFilePath), null); 
    resourceIdView.setImageDrawable(d); 
} 
catch(Exception e) 
{ 
    System.out.println(" Error"); 

} 

希望這會有所幫助。

1

把你的圖像中繪製文件夾,並設置成ImageView的...

resourceIdView.setImageResource(R.drawable.AUD); 
+0

我有圖像列表,我有一個自定義的數組適配器來填充ListView.How可以用main.xml連接列表嗎? – vikifor

+0

看到這個http://samir-mangroliya.blogspot.in/p/android-image-listview.html –

0

我所擁有的是圖像在Designer中顯示,而不是在設備上顯示,所以我將圖像放在所有drawable-xdpi目錄中。這對我有效。