2012-02-24 82 views
0

我有一個包含幾張圖片的文件夾,這些圖片是由用戶動態添加的。所以我需要在Android中獲得這些圖像的R.drawable ID。在android中動態獲取R.drawable ID

+0

動態是指? – 2012-02-24 18:07:14

+0

用戶將圖像放在文件夾 – 2012-02-26 07:22:03

回答

0

你還要保存這些圖像的位置?當圖像在運行時動態添加時,通常需要將它們存儲在手機存儲器中。如果你想將圖像存儲在外部存儲器(SDCard)中,那麼你可以使用下面的代碼來檢索它們並將它們添加到你的視圖中(我假設你在做)。此代碼假定圖像正被存儲到您的設備SDCard中,這是它們將從中取出的位置。

//Get root directory of storage directory, pass in the name of the Folder if they are  being stored farther down, i.e getExternalFilesDir("FolderName/FolderName2") 

String state = Environment.getExternalStorageState(); 

if (Environment.MEDIA_MOUNTED.equals(state)) { 
// We can read and write the media 


File dir = getExternalFilesDir(null); 
    Bitmap bmap = null; 
    try { 
     InputStream is = new FileInputStream(new File(dir,"image.jpg")); 
     bmap = BitmapFactory.decodeStream(is); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    if (bmap!=null) { 
     ImageView view = new ImageView(this); 
     view.setImageBitmap(bmap); 
    } 
} 
+0

但我需要獲取圖像視圖中圖像的R.drawable.ID – 2012-02-26 07:22:51

+1

運氣不好,任何動態添加的內容都不會被轉換爲R.drawable.id編譯後。 – Danuofr 2012-04-05 20:28:07

+0

嗯亞對 對於遲到的回覆感到抱歉 – 2012-05-26 17:03:36