2011-09-07 48 views
1

我試圖讓用戶選擇使用設備默認相機拍照並從圖像庫中選擇也默認爲設備。來自Gallery和Camera-taken-photo的URI/URL路徑不同

我可以讓相機拍照並將其顯示在應用程序中,因爲它似乎喜歡將URI路徑直接傳輸到JPG文件。然而,給畫廊URI的路徑是非常不同的,根本不顯示圖像。

這裏是pathes我得到:

當來自CAMERA的措施: /mnt/sdcard/filename.jpg

當來自畫廊選擇的範圍: /外部/圖像/媒體/ #(#爲ID號/縮略圖我相信)

用於檢索二者pathes的代碼是:

CAMERA

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      mImageCaptureUri = 
      Uri.fromFile(new file(Environment.getExternalStorageDirectory(), 
      "fname_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 

畫廊

Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, 
      "Complete action using"), PICK_FROM_FILE); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 

隨着畫廊,它打開,我可以瀏覽就好了,它只是因爲它與服用的確不顯示圖像圖片。

用於圖像拉進我的應用程序,一旦選定/採取的代碼是:

ImageView getMyphoto = (ImageView) findViewById(R.id.usePhoto); 
    String stringUrl = prefSettings.getString("myPic", ""); 
    Uri getIMG = Uri.parse(stringUrl); 
    getMyphoto.setImageURI(null); 
    getMyphoto.setImageURI(getIMG); 

回答

2

檢查URI中的字符串「/外部」,然後用得到正確的路徑方法來獲取絕對路徑。

private String getRealPathFromURI(Uri contentUri) { 
     int columnIndex = 0; 

     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(contentUri, proj, null, null, null); 

     try { 
      columnIndex = cursor.getColumnIndexOrThrow 
          (MediaStore.Images.Media.DATA); 
     } catch (Exception e) { 
      Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI", 
          Toast.LENGTH_SHORT).show(); 
      finish(); 
      return null; 
     }  
     cursor.moveToFirst(); 
     return cursor.getString(columnIndex);    
    } 
+0

虐待這個試試看,謝謝。 – livingpaint