2014-02-13 79 views
-1

我在我的活动中有2个按钮。第一个:上传图像第二个:获取上传的图像名称。现在我按了“Upload Image”按钮并从图库中选择了一个图像,该图像显示在ImageView中。然后我按下第二个按钮“获取上传的图像名称”,Toast将显示ImageView中该图像的名称。我只想要这个代码。如何在Android中获取图像名称?

回答

0
Uri uri = data.getData(); 
    String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID, 
      MediaStore.Images.Media.BUCKET_DISPLAY_NAME }; 

    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 

    ArrayList<String> ids = new ArrayList<String>(); 
    mAlbumsList.clear(); 
    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      Album album = new Album(); 

      int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID); 
      album.id = cursor.getString(columnIndex); 

      if (!ids.contains(album.id)) { 
       columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 
       album.name = cursor.getString(columnIndex); 

       columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID); 
       album.coverID = cursor.getLong(columnIndex); 

       mAlbumsList.add(album); 
       ids.add(album.id); 
      } else { 
       mAlbumsList.get(ids.indexOf(album.id)).count++; 
      } 
     } 
     cursor.close(); 
+0

感谢爵士答复。通过使用上面的代码,我可以获得图库中的所有图像名称吗? –

+0

是............. –

+0

先生,我只是想要我上传到ImageView的图像的名称。 –

0

这是你可以检索图像名称的方式..:

Uri selectedImage = data.getData(); 
String selectedImageName = selectedImage.getLastPathSegment();