25

Hi I am using ACTION_IMAGE_CAPTURE for capturing image using Intent as follows:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT, (new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg"))
);
startActivityForResult(cameraIntent, 0);

I need to store image in an sdcard and retrieve the path of that image using the onActivityResult method. I don't know how to get the image path of the currently captured image.

If any one knows please help.

Thanks

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
iCoder86
  • 1,874
  • 6
  • 26
  • 46
  • 2
    please put all of your code within the "code" textboxes, it makes reading your code much easier, so than we may be able to answer your questions – Samuel Nov 15 '10 at 14:19

4 Answers4

50

This is how it works on 2.2 (different than on previous versions). When starting intent

        String fileName = "temp.jpg";  
        ContentValues values = new ContentValues();  
        values.put(MediaStore.Images.Media.TITLE, fileName);  
        mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
        startActivityForResult(intent, CAPTURE_PICTURE_INTENT);

you need to remember mCapturedImageURI.

When you capture image, in onActivityResult() use that URI to obtain file path:

            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);

UPDATE: On new Android devices you would not need MediaStore.EXTRA_OUTPUT, but you rather deduce image/video URI from data.getData() received from onActivityResult(..., Intent data), as nicely described under

Android ACTION_IMAGE_CAPTURE Intent

However, since this part is subject to manufacturer adaptation, you may still encounter phones where "old" approach may be useful.

Community
  • 1
  • 1
Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • 1
    Thank you for this, but does not work on Samsung Galaxy I or II. Returns thumbnail on HTC Incredible :-( – Oh Danny Boy Oct 03 '11 at 17:55
  • After taking the photo activity is reloaded & while starting my application, it shows "Waiting for Debugging....". Is anything I missed. Check the code here: http://stackoverflow.com/questions/10510547/android-display-captured-image-in-img-tag – Ponmalar May 09 '12 at 11:34
  • From onActivityResult() you may obtain what was the real name on the SD card. capturedImageFilePath has it. – Zelimir May 09 '12 at 11:35
  • Glad that I could help. Honestly, I am also not sure how does it work on all post 2.2 platforms, so any additional info is also valuable for me. Regards. – Zelimir Sep 13 '12 at 10:36
  • I got error in this code. What to pass CAPTURE_PICTURE_INTENT ? – Nirav Ranpara Nov 28 '12 at 06:28
  • @Zelimir : Can you please help me . I post my question here >http://stackoverflow.com/questions/13599201/get-error-when-capture-image – Nirav Ranpara Nov 28 '12 at 06:52
  • @Nirav Ranpara - I think you already got the answer in your new question track. CAPTURE_PICTURE_INTENT is in fact constant value (e.g. 1), used to distinguish this request from some other sent from your Activity. That constant should be used in the onActivityResult() callback. – Zelimir Nov 28 '12 at 07:45
  • @Nirav Ranpara - On which Android platform version are you running your code? Below 2.2 it works differently. Have you added permissions for Camera and writing of the xternal storage? – Zelimir Nov 28 '12 at 07:52
  • 1
    Thanks for the answer. `managedQuery()` is deprecated now however, as mentioned [here](http://stackoverflow.com/a/7653634/1446598). [Here](http://stackoverflow.com/a/7469366/1446598) is a simple alternative; namely use `context.getContentResolver()` instead; where `context` is a `Context` instance. – Noha Kareem Jan 28 '13 at 09:26
  • but same image saves 2 time bydefault name and which we manually created file name – Pranita Patil Jul 26 '13 at 11:04
7

Another way, tested on android 2.1, is take the ID or AbsolutePath of the gallery last image.

It can be done like that:

/**
 * Gets the last image id from the media store
 * @return
 */
private int getLastImageId(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "getLastImageId::id " + id);
        Log.d(TAG, "getLastImageId::path " + fullPath);
        imageCursor.close();
        return id;
    }else{
        return 0;
    }
}

And to remove the file:

private void removeImage(int id) {
       ContentResolver cr = getContentResolver();
        cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}

This code was based on the post: Deleting a gallery image after camera intent photo taken

Community
  • 1
  • 1
Derzu
  • 7,011
  • 3
  • 57
  • 60
2

This question is very old but I have been battling with the same problem for half a day. The issue is that your ACTION_IMAGE_CAPTURE intent will always return code=-1 and data=null unless you set the following permissions for the application in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA"/>

You can also set the following if you want to record audio from your application:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Lukuluba
  • 381
  • 1
  • 5
  • 18
2

Remove

intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  

and add

Uri uri = data.getData();in onActivityResult
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
ahmad
  • 21
  • 1