2011-01-29 35 views
1

我有這個imageview對我的佈局:爲什麼這ImageView不顯示我的圖像?

<ImageView 
    android:id="@+id/profileImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@id/profileImageLabel" 
    android:layout_alignParentRight="true" 
    android:maxWidth="90px" 
    android:maxHeight="90px"/> 

,我給它的圖像源動態上的onCreate():

Bitmap bm = BitmapFactory.decodeFile("c:/logo.png"); 

     profileImage.setImageBitmap(bm); 
     profileImage.invalidate(); 

但是當我測試的應用程序,它不不顯示圖像,只顯示黑屏。

爲什麼imageview不能顯示,只有黑屏?

回答

2

嗯,它接縫我,你的文件路徑是錯誤的。你正在模擬器中測試它嗎?仿真器無法訪問您的硬盤或其他任何不屬於仿真器的部分。 你應該做的是你的圖像添加到您的繪製文件夾在Eclipse(如果你使用的是什麼,多數民衆贊成),然後通過文字加載文件:

Resources r = this.getResources(); 
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.logo); 
+0

以及如果我需要從手機中獲取文件會發生什麼? (我需要這個到我的應用程序,因爲用戶可以從他們的手機中選擇一個圖像並用作配置文件圖像) – NullPointerException

+0

那麼你可以按照你所做的方式,只需輸入正確的文件路徑。你可以在這裏閱讀更多關於這個:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – Durza007

0

可以使用塊這樣

File iconFile = new File("/path/to/image.png"); 
FileInputStream fis = new FileInputStream(iconFile); 
Bitmap bi = BitmapFactory.decodeStream(fis); 
fis.close(); 

獲取您在「磁盤上」的文件的位圖。 /path/to/image.png是圖像存儲的路徑。