2011-09-07 75 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