2012-02-24 85 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