来自Gallery和Camera-taken-photo的URI / URL路径不同

时间:2011-09-07 04:26:26

标签: android path camera android-gallery android-imageview

我试图让用户在使用设备默认相机拍照并从图库中选择默认设备之间进行选择。

我可以让相机拍照并让它在应用程序中显示得很好,因为它似乎喜欢直接转到JPG文件的URI。但是,图库URI的路径非常不同,根本不显示图像。

以下是我得到的补丁:

从相机拍摄时: /mnt/sdcard/filename.jpg

从画廊选择时: / external / images / media /#(#是我认为的ID号/缩略图)

用于检索两个路径的代码是:

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);

1 个答案:

答案 0 :(得分:2)

检查uri字符串中的“/ external”,然后使用get path path方法获取绝对路径。

 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);               
    }