18

I want to make a picture with the camera intent and save it to the default DCIM folder. Then I want to get the path/filename where the picture is stored.

I am trying it with the following code:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE); 

With this code, the camera opens and after I have taken one picture it closes and saves the picture to the default image folder (usually /dcim/camera or sdcard/dcim/camera...)

but how can I get the path and filename of the taken picture now? I have tried almost everything in onActivityResult I tried

String result = data.getData(); 

and

String result = data.getDataString(); 

and

String result = data.toURI(); 

and

Uri uri = data.getData(); 

etc.

I researched the last two days to find a solution for this, there are many articles on the web and on stackoverflow but nothing works. I don't want a thumbnail, I only want the path (uri?) to the image that the camera has taken.

Thank you for any help

EDIT: When I try:

path=Environment.DIRECTORY_DCIM + "/test.jpg"; File file = new File(path); Uri outputFileUri = Uri.fromFile(file); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE); 

it does not store the image as test.jpg but with the normal image name 2011-10-03.....jpg (but that is ok too, i only need the path to the image, it does not matter what the name is).

Best regards

EDIT Again

path=Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/"; File file = new File(path,"test111111111.jpg"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } Uri outputFileUri = Uri.fromFile(file); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE); 

When I try this, it stores the image to the right folder and with the given name (e.g. test111111.jpg). But how can I get the filepath now in onActivityResult?

1
  • 6
    you allready know the path... since you create your file. you can keep the file or the path as a member variable of your Activity Commented Oct 3, 2011 at 18:10

3 Answers 3

6

The picture will be stored twice, first on gallery folder, and after on the file you especified on putExtra(MediaStore.EXTRA_OUTPUT, path) method.

You can obtain the last picture taken doing 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; } } 

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey , This code works on samsung tab but doesn't work on lenovo and i-ball tab, is there any other solution ?????
1

you can use like this in onActivityResult()

if(requestCode==CAMERA_CAPTURE) { Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC"); if(cursor != null && cursor.moveToFirst()) { do { String picturePath =cursor.getString(cursor.getColumnIndex(Media.DATA)); Uri selectedImage = Uri.parse(picturePath); } while(cursor.moveToNext()); cursor.close(); File out = new File(picturePath); try { mOriginal = decodeFile(out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mSelected.setImageBitmap(mOriginal); } } 

Comments

0

When you are starting the ACTION_IMAGE_CAPTURE you can pass an extra MediaStore.EXTRA_OUTPUT as the URI of the file where you want to save the picture.

Here is a simple example:

 File file = new File(path); Uri outputFileUri = Uri.fromFile(file); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE); 

EDIT: I just tried on my device and file.createNewFile() solved the problem for me.

3 Comments

Hi,thank you for your quick reply, but it doesn't work as expected. I will explain above.
a silly question maybe... have you added the permission in the Manifest? android.permission.CAMERA? yes..it can work without permission some times
yes, I added the permission. I just need to know the path to the picture that the camera has saved. and i know that the path is the default picture folder, so actually i only need the file name or the path to the latest picture taken. i am looking two days for a solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.