2011-06-03 81 views
26

我有一張圖片存儲在我手機的SD卡中。我想在圖像視圖中顯示它。我知道文件的位置。在活動的OnCreate有一個簡單的方法來這樣說設置Imageview在SD卡中顯示圖像?

ImageView img = (ImageView) findViewById(R.id.imageView1);  
String path = Environment.getExternalStorageDirectory().toString() "Images/image.jpg";  
img.setsrc = path ; 

請讓我知道,如果有任何的方式來做到這一點。謝謝。

回答

83
Bitmap bmp = BitmapFactory.decodeFile(pathName); 
ImageView img; 
img.setImageBitmap(bmp); 

希望這會有所幫助。

+0

我有問題加載圖像大小1.98 MB,相機照片尺寸16M像素的位圖。 – 2015-11-03 09:49:52

+1

如果在ImageView上不斷創建和設置,我們是否需要在位圖上調用回收? – Ahmed 2016-01-12 22:56:29

+0

當活動結束時,圖像會丟失,無論如何使它永久存在,還是必須將文本的路徑字符串作爲文本寫入sdcard並且每次都從中讀取? – user3560827 2016-06-28 16:32:42

20

我有這樣的片段,可以幫助:

File imageFile = new File("/sdcard/example/image.jpg"); 
if(imageFile.exists()){ 
    ImageView imageView= (ImageView) findViewById(R.id.imageviewTest); 
    imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath())); 
} 

:)