2010-07-31 75 views
1

我目前正在製作一個適用於圖像的應用程序。我需要在用戶選擇存儲在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(這是在下一個活動中發生的情況)。但是,我不知道該怎麼做。

+0

我不是100%在這個,但我想我做了一些像 selectedImage.getPath(); – stealthcopter 2010-07-31 16:26:53

回答

2

我已經找到了我自己的問題的答案。在做了一些更多的搜索之後,我終於偶然發現了SO這裏的一個帖子,它在這裏提出了同樣的問題:android get real path by Uri.getPath()

不幸的是,答案有一個斷開的鏈接。經過一番Google搜索之後,我在這裏找到了正確的鏈接:http://www.androidsnippets.org/snippets/130/(我已經驗證過這段代碼確實有效。)

但是,我決定採取不同的路線。由於我的下一個活動正在使用ImageView來顯示圖片,因此我將對鏈接到下一個活動的所有方法使用Uri內容字符串。

在下一個活動中,我使用的是ImageView.setImageUri()方法。

這裏是我在接下來的活動做從content://字符串顯示圖片代碼:

// Get the content string from the previous Activity: 
picLocation = getIntent().getStringExtra("PICTURE_LOCATION"); 

// Instantiate the ImageView object: 
ImageView imageViewer = (ImageView)findViewById(R.id.ImageViewer); 

// Convert the Uri string into a usable Uri: 
Uri temp = Uri.parse(picLocation); 
imageViewer.setImageURI(temp); 

我希望這個問題的答案將有助於未來的Android開發。

2

這裏,我希望有人認爲有用的另一個答案:

您可以在MediaStore任何內容做到這一點。在我的應用程序中,我必須從URI獲取路徑並從路徑獲取URI。前者:

/** 
* Gets the corresponding path to a file from the given content:// URI 
* @param selectedVideoUri The content:// URI to find the file path from 
* @param contentResolver The content resolver to use to perform the query. 
* @return the file path as a string 
*/ 
private String getFilePathFromContentUri(Uri selectedVideoUri, 
     ContentResolver contentResolver) { 
    String filePath; 
    String[] filePathColumn = {MediaColumns.DATA}; 

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    filePath = cursor.getString(columnIndex); 
    cursor.close(); 
    return filePath; 
} 

後者(這是我對影片做的,但也可以通過對MediaStore.Video代MediaStore.Audio(ETC),用於音頻或文件或其他類型的存儲內容:

/** 
* Gets the MediaStore video ID of a given file on external storage 
* @param filePath The path (on external storage) of the file to resolve the ID of 
* @param contentResolver The content resolver to use to perform the query. 
* @return the video ID as a long 
*/ 
private long getVideoIdFromFilePath(String filePath, 
     ContentResolver contentResolver) { 


    long videoId; 
    Log.d(TAG,"Loading file " + filePath); 

      // This returns us content://media/external/videos/media (or something like that) 
      // I pass in "external" because that's the MediaStore's name for the external 
      // storage on my device (the other possibility is "internal") 
    Uri videosUri = MediaStore.Video.Media.getContentUri("external"); 

    Log.d(TAG,"videosUri = " + videosUri.toString()); 

    String[] projection = {MediaStore.Video.VideoColumns._ID}; 

    // TODO This will break if we have no matching item in the MediaStore. 
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(projection[0]); 
    videoId = cursor.getLong(columnIndex); 

    Log.d(TAG,"Video ID is " + videoId); 
    cursor.close(); 
    return videoId; 
} 

基本上,MediaStoreDATA列(或任何小節它要查詢)存儲的文件路徑,讓你用你知道什麼來查找資料,或者您查詢的DATA字段選擇你關心的內容。