1

I am trying to get the users to select between taking a picture with the device default camera and select from the gallery of images also default to the device.

I can get the camera to take the picture and have it display within the app just fine since it seems to like the URI pathing straight to a JPG file. However, the pathing given to the gallery URI is very different and does not display the image at all.

Here are the pathes I get:

WHEN TAKEN FROM CAMERA: /mnt/sdcard/filename.jpg

WHEN CHOSEN FROM GALLERY: /external/images/media/# (# is the ID number/thumbnail I believe)

The code used for retrieving both pathes are:

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

GALLERY:

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

With the Gallery, it opens and I can browse just fine, it just doesn't display the image as it does with taking the picture.

The code used for pulling the images into my app once selected/taken is:

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

1 回答 1

2

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

 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);               
    }
于 2011-09-07T05:34:35.830 回答