我目前正在製作一個適用於圖像的應用程序。我需要在用戶選擇存儲在SD卡上的文件時實現功能。一旦他們選擇了圖片(使用Android圖庫),圖像的文件位置將被髮送到另一個活動,其中將完成其他工作。在Android中獲取使用的圖像文件位置
我在這裏看過類似的帖子,但沒有專門回答我的問題。基本上,這是當用戶點擊「加載圖片」按鈕,我做的代碼:
// Create a new Intent to open the picture selector:
Intent loadPicture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// To start it, run the startActivityForResult() method:
startActivityForResult(loadPicture, SELECT_IMAGE);
從這個代碼,然後我有一個onActivityResult()
方法聽回撥:
// If the user tried to select an image:
if(requestCode == SELECT_IMAGE)
{
// Check if the user actually selected an image:
if(resultCode == Activity.RESULT_OK)
{
// This gets the URI of the image the user selected:
Uri selectedImage = data.getData();
// Create a new Intent to send to the next Activity:
Intent i = new Intent(currentActivty.this, nextActivity.class);
// ----------------- Problem Area -----------------
// I would like to send the filename to the Intent object, and send it over.
// However, the selectedImage.toString() method will return a
// "content://" string instead of a file location. How do I get a file
// location from that URI object?
i.putExtra("PICTURE_LOCATION", selectedImage.toString());
// Start the activity outlined with the Intent above:
startActivity(i);
如上面的代碼所示,uri.toString()
將返回一個content://
字符串,而不是所選圖片的文件位置。我如何獲取文件位置?
注意:另一個可能的解決方案是發送content://
字符串並將其轉換爲Bitmap
(這是在下一個活動中發生的情況)。但是,我不知道該怎麼做。
我不是100%在這個,但我想我做了一些像 selectedImage.getPath(); – stealthcopter 2010-07-31 16:26:53