2011-12-07 97 views
2

我編寫了一些拍攝照片的代碼,我想查找剛拍攝的位圖文件的名稱。如何找到剛剛拍攝的位圖文件的名稱

我知道如何獲取位圖 - 但我不想獲取位圖 - 我只需要保存在SD卡上的文件的名稱。當你得到的結果

//define the file-name to save photo taken by Camera activity 
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState) 
mImageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues()); 
Intent photoFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
photoFromCamera.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); 
startActivityForResult(photoFromCamera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

然後:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == 0) 
    {  
     // This is the file name that i need to find ... 
     Bitmap image = (Bitmap) data.getExtras().get("data"); 

回答

1

你可以做到這一點

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode){ 
    case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE: 
     if (resultCode == RESULT_OK) { 
      Log.i(TAG, "Camera: "+ mImageUri); 
      String path = getRealPathFromURI(mImageUri); 
      Log.d(TAG, "Result image path from camera: "+path); 

     } 
     break; 

輔助方法:

private String getRealPathFromURI(Uri contentUri) { 

    // can post image 
    String [] proj={MediaStore.Images.Media.DATA}; 
    Cursor cursor = managedQuery(contentUri, 
        proj, // Which columns to return 
        null,  // WHERE clause; which rows to return (all rows) 
        null,  // WHERE clause selection arguments (none) 
        null); // Order-by clause (ascending by name) 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    String res = cursor.getString(column_index); 
    cursor.close(); 

    return res; 
} 
+0

對不起,這是行不通的( – Yanshof

+0

)它拋出一個異常嗎?或者你只是沒有得到的價值。嘗試讓圖像上的光標,但傳遞null作爲投影,然後使用DatabaseUtils.dumpCursorToString()檢查查看Android的存儲位置的列 –

+0

未知的異常被丟棄 – Yanshof

0

您可以通過使用FileObserver媒體或新照片將被存儲的目錄。 我希望這有助於!

+0

好的,我該如何使用它? – Yanshof

相關問題