2012-07-21 79 views
7

下面是我的代碼,但沒有給我映像路徑在onActivity結果如何從相機拍攝的只是圖像路徑

Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       Log.w("jay", "Camera Image Path :" + selectedImagePath); 

       Toast.makeText(MiscansOther_pannel.this, selectedImagePath, 
         Toast.LENGTH_LONG).show(); 

回答

40

這對我的作品......

代碼:

Uri selectedImageUri = data.getData(); 
selectedImagePath = getRealPathFromURI(selectedImageUri); 

方法:getRealPathFromURI()

//---------------------------------------- 
    /** 
    * This method is used to get real path of file from from uri 
    * 
    * @param contentUri 
    * @return String 
    */ 
    //---------------------------------------- 
    public String getRealPathFromURI(Uri contentUri) 
    { 
     try 
     { 
      String[] proj = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     catch (Exception e) 
     { 
      return contentUri.getPath(); 
     } 
    } 

編輯:

正如我在一些設備拍攝的圖像後onActivityResult()數據是注意到,

所以另一種方法,傳遞特定的圖像文件名作爲參數傳遞給你的意圖的捕獲圖像爲putExtra參數。

隨後也插入這個圖像的URI媒體商店,現在使用這個開放的爲您進一步的使用,

您可以檢查圖像是否被拍攝或不File.exist()

碼的樣子,

現在
ContentValues values = new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE, "Image File name"); 
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE); 

,您可以使用URI中獲取文件路徑相同的方法,

在這種情況下,它會在onActivityResult()

selectedImagePath = getRealPathFromURI(mCapturedImageURI); // don't use data.getData() as it return null in some device instead use mCapturedImageUR uri variable statically in your code, 
+1

一個巨大的thanx兄弟......它的完美工作mybro – 2012-07-21 11:50:47

+4

除非你使用的是三星。 – 2012-07-21 12:43:30

+0

@OvidiuLatcu - 我沒有在三星上測試過。三星設備使用此代碼時出現什麼問題? – user370305 2012-07-21 16:55:49

相關問題