0

I am using camera Intent to open the camera:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

Now I am trying to get the path of the captured photo but its throwing error:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Uri uri = data.getData();
String imagePath = getRealPathFromURI(uri);
}
}

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
// import android.support.v4.content.CursorLoader; i import this for CursorLoader
    CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Its throwing Error which is:

FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100,     result=-1, data=Intent { act=inline-data (has extras) }} to activity  {com.xxx.xxx.BbmpActivity}: java.lang.NullPointerException

at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
at android.os.Handler.dispatchMessage(Handler.java:99)  
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)    
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)     
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException   
at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)    
at android.content.ContentResolver.query(ContentResolver.java:256)
at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
at com.pixel.bbmp.BbmpActivity.getRealPathFromURI2(BbmpActivity.java:546)
at com.pixel.bbmp.BbmpActivity.onActivityResult(BbmpActivity.java:491)  
at android.app.Activity.dispatchActivityResult(Activity.java:3908)  
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)

I think because of this line it throwing error:

CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);

here what i need to give instead of getApplicationContext() I also try this but till it throwing error:

CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);

when I start this application then it throw one error which is:

Could not find class 'android.content.CursorLoader', referenced from method com.xxx.xxx.BbmpActivity.getRealPathFromURI
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

3 Answers3

1

Instead of Uri uri = data.getData();write like..

if(data!=null){
  if (requestCode == CAMERA_REQUEST) {  
           Uri uri =  data.getExtras().get("data"); 
         //or...
     Bitmap bitmapPicture = (Bitmap) data.getExtras().get("data"); 
     // other code
}
}

It will work fine. main thing is data.getExtras().get("data");

mainu
  • 448
  • 2
  • 11
  • its showing error: Type mismatch: cannot convert from Object to Uri for Uri uri = data.getExtras().get("data"); And if I write Bitmap bitmapPicture = (Bitmap) data.getExtras().get("data"); then how i send uri as an argument to getRealPathFromURI method – Vinit ... Oct 29 '12 at 12:00
  • u can compress the bitmap and then send it to the stream in byte form – mainu Oct 29 '12 at 12:02
  • my problem is how I get the path. Uri uri = data.getExtras().get("data"); this line throwing error. which is Type mismatch: cannot convert from Object to Uri – Vinit ... Oct 29 '12 at 12:06
  • 2
    for the line: Uri uri = data.getExtras().get("data"); In the logcat it throwing error: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.xxx.xxx/com.xxx.xxx.BbmpActivity}: java.lang.ClassCastException: android.graphics.Bit – Vinit ... Oct 29 '12 at 12:20
  • 1
    The intent data object passed as parm to onActivityResult() can be null, even though the camera took the picture and saved it to storage. It's a PITA. There are numerious posts about this, including at least one bug report to Google/Android as some people (like me) consider this a bug, not an oversight. You would (and should) expect the camera to tell you where to find that photo you just took, otherwise how could you even use it? Pretty lame. – Howard Pautz May 20 '13 at 22:48
0
CursorLoader loader = new CursorLoader(getApplicationContext(), 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
proj, null, null, null);
Howard Pautz
  • 415
  • 7
  • 21
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • @HowardPautz: have you got an idea why the original question was deleted? – Alex Cohn May 09 '13 at 06:56
  • Sorry, I don't remember what it said about the link that was dead - was just cleaning up. I came across this because of Samsung devices not reading files from /storage/sdcard0 ... – Howard Pautz May 09 '13 at 16:06
0

You can specify a URI before when setup the capture intent :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.mCameraFile));
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Where mCameraFile is a file I created :

this.mCameraFile = File.createTempFile("toto", "jpg", fAlbum);

Where fAlbum, is a File that represent my album folder :

this.fAlbum = Environment.getExternalStorageDirectory() + "/dcim/" + "tata";
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124