steprobe
steprobe

Reputation: 1699

Retrieving a Content Uri from Camera Intent

I am firing an Intent for the camera to take a photo as follows:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri myPhoto = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
    "the_photo.jpg"));

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myPhoto);
startActivityForResult(cameraIntent, INTENT_GET_PICTURE_FROM_CAMERA);

Now I want to get the thumbnail for this photo to show. If I store the "myPhoto" Uri, I can see exactly the file on the file system. However, to use the Media Store API, I need the Uri on the Content Uri form. Is there anyway to retrieve this from the intent the camera gives back and if not how can I convert my file system Uri to the android content Uri.

Note, I know I can just take the bitmap and decode it, downsampling as I go. This is not what I want. I can query all the thumbnails and I see it exists, so I want to get it from the system.

Thanks

Upvotes: 2

Views: 1113

Answers (2)

Jordi
Jordi

Reputation: 616

AS i commented, i needed something similar (to get " content://media/external/images/xxx " uri format) and your solution worked pretty well. Thanks you! But i found a shorter way to do it (maybe it doesn't work for you, but for someone else).

Using the same intent formulation as yours (first post), but with the "CAMERA_REQUEST"

inside onActivityResult

    getContentResolver().notifyChange(mCapturedImageURI, null);
    ContentResolver cr = getContentResolver();
    Uri uriContent = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo.getAbsolutePath(), null, null));

and getting the 'uriContent' in 'content://media/external/images/xxx' format.

Upvotes: 0

steprobe
steprobe

Reputation: 1699

I resolved it by performing a Media Scan on the item. From there I could get the content Uri (the extra complication of the handler in the code below is because I needed my result to be handled on the UI thread as it manipulated views):

private Uri mCameraFilePath;
private Context mContext;
...

private void handlePictureFromCamera(Intent data) {

    MyMediaScannerClient client = new MyMediaScannerClient(
            mCameraFilePath.getPath(), new Handler(), new OnMediaScanned() {

        @Override
        public void mediaScanned(Uri contentUri) {
            //Here I have the content uri as its now in the media store
        }
    });
    MediaScannerConnection connection = new MediaScannerConnection(mContext, client);
    client.mConnection = connection;
    connection.connect();
}

private interface OnMediaScanned {
    public void mediaScanned(Uri contentUri);
}

/**
 * This is used to scan a given file (image in this case) to ensure it is added
 * to the media store thus making its content uri available. It returns the
 * content form Uri by means of a callback on the supplied handler's thread.
 */
private class MyMediaScannerClient implements MediaScannerConnectionClient {

    private final String mPath;
    private MediaScannerConnection mConnection;
    private final OnMediaScanned mFileScannedCallback;
    private final Handler mHandler;

    public MyMediaScannerClient(String path, Handler handler, OnMediaScanned fileScannedCallback) {
        mPath = path;
        mFileScannedCallback = fileScannedCallback;
        mHandler = handler;
    }

    @Override
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, null);
    }

    @Override
    public void onScanCompleted(String path, final Uri uri) {

        mConnection.disconnect();
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                mFileScannedCallback.mediaScanned(uri);
            }
        });
    }
}

Upvotes: 2

Related Questions