开发者

URI/URL Paths from Gallery and Camera-taken-photo are different

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_OUT开发者_如何学PythonPUT, 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);


Check for the "/external" in the uri string and then use get the right path method to get the absolute 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);               
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜