Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Image Path just captured from camera

below is my code but is not give me image path in onActivity result

Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Log.w("jay", "Camera Image Path :" + selectedImagePath);

                Toast.makeText(MiscansOther_pannel.this, selectedImagePath,
                        Toast.LENGTH_LONG).show();
like image 421
Jaykumar Donga Avatar asked Jul 21 '12 11:07

Jaykumar Donga


People also ask

How do I find a photo path?

Hold down the Shift key, then right-click the photo. In the context menu that appears, find and click Copy as path. This copies the file location to the clipboard.

How to get camera image Path in Android?

The URI path for the image returned by camera is fetched inside the method getCaptureImageOutputUri() . The onActivityResult essentially returns a URI to the image. Some devices do return the bitmap as data. getExtras().

How do you open camera through intent and display captured image?

Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.


1 Answers

This works for me...

Code:

Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);

Method: getRealPathFromURI()

//----------------------------------------
    /**
     * This method is used to get real path of file from from uri
     * 
     * @param contentUri
     * @return String
     */
    //----------------------------------------
    public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }

EDIT:

As I noticed in some device after captured image the data in onActivityResult() is null,

So the alternate way, Pass the specific image filename as a argument to your Intent for capture image as putExtra parameter.

Then also insert this image Uri in Media Store, Now use this Uri for your further use,

You can check whether image is captured or not by File.exist(),

Code looks like,

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

Now, you can use the same method for get file path from Uri,

in this case it will be in onActivityResult(),

selectedImagePath = getRealPathFromURI(mCapturedImageURI); // don't use data.getData() as it return null in some device instead  use mCapturedImageUR uri variable statically in your code,
like image 142
user370305 Avatar answered Sep 17 '22 21:09

user370305