6

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();
Jaykumar Donga
  • 380
  • 1
  • 3
  • 18

1 Answers1

41

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,
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 4
    except if you are using a Samsung. – Ovidiu Latcu Jul 21 '12 at 12:43
  • @OvidiuLatcu - I didn't tested it on Samsung. What is the problem in Samsung device using this code? – user370305 Jul 21 '12 at 16:55
  • Hm, i know its been over a year, but when i use the code you posted the image i take gets saved 2x, once in default DCIM/100MEDIA folder where all camera shots go and once in DCIM/CAMERA folder...and most importantly the image doesnt get the name i specified as "Image File Name"...it just gets a random number. – JanBo Nov 20 '13 at 21:00
  • I would like to say that for me this was giving RunTimeException. I solved using: ``intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, getRealPathFromURI(mCapturedImageURI));`` - like, passing the String, not URI. – Victor Oliveira Jun 13 '14 at 14:08
  • data.getData(); onActivityResult() will give thumbnail only – HendraWD Apr 21 '16 at 03:21
  • @user370305 if is it possible to give you 1000 upvots, sure i will give you,I was looking for this type of solutions. Thank you very much, using your code now i am able to send my image taken by camera to remote server with compress and without loose of image quality. – Hiren Nov 14 '16 at 06:18