3

i work with android 2.1 , and i want to get real path from Camera intent result. I read Get Path of image from ACTION_IMAGE_CAPTURE Intent but it is for android 2.2.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == CAMERA_RESULT)
    {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        imv.setImageBitmap(thumbnail);
         Uri selectedImageUri = data.getData();
         String path = getRealPathFromURI(selectedImageUri);
    }
}

private 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();
    }
}
Community
  • 1
  • 1
Kostya Khuta
  • 1,846
  • 6
  • 26
  • 48
  • What problem you have in this?? – Ajay S Mar 10 '13 at 13:38
  • java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.cameratest/com.example.cameratest.MainActivity}: java.lang.NullPointerException – Kostya Khuta Mar 10 '13 at 13:52
  • Debug your application `selectedImageUri` is NULL and let me know if this.. – Ajay S Mar 10 '13 at 13:55
  • Uri selectedImageUri = data.getData(); if (selectedImageUri == null) { Log.d(LOG_TAG, "Yes"); } Yes, it is null – Kostya Khuta Mar 10 '13 at 14:01

2 Answers2

7

Its above code works in some mobile but does not work in samsung mobile in my case so I implemented the common logic for all devices.

When I capture the photo from camera so I implement a logic using Cursor and iterate the cursor and get the last photo path which is capture from camera.

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 {
        uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
        photoPath = uri.toString();
    }while(cursor.moveToNext());
    cursor.close();
}
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • In case of Samsung S5, image returned will usually be of high quality. How can I compress this image if I need to upload this to server? – Shubham A. Oct 14 '15 at 15:39
5

The answer given by @TGMCians works but i was able to improvise it further as below

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.moveToLast()){
    Uri fileURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
    String fileSrc = fileURI.toString();
    cursor.close();
}
Dhir Pratap
  • 1,188
  • 11
  • 14