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(); } } 
4
  • What problem you have in this?? Commented Mar 10, 2013 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 Commented Mar 10, 2013 at 13:52
  • Debug your application selectedImageUri is NULL and let me know if this.. Commented Mar 10, 2013 at 13:55
  • Uri selectedImageUri = data.getData(); if (selectedImageUri == null) { Log.d(LOG_TAG, "Yes"); } Yes, it is null Commented Mar 10, 2013 at 14:01

2 Answers 2

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(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.