I need to take a photo with the camera using an activity. In my onActivityResult I am able to retrieve an uri, but I cannot get the file of this uri by using the File constructor. My intent looks like this:
Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageURICamera = Uri.fromFile(photo);
startActivityForResult(cameraintent, 1);
I use the global defined “imageURICamera” in onActivityResult. I think that the problem has something to do with the Uri not having the proper attributes as the documentation for the file constructor states that:
“Constructs a new File using the path of the specified URI. uri needs to be an absolute and hierarchical Unified Resource Identifier with file scheme and non-empty path component, but with undefined authority, query or fragment components”
My onActivityResult looks like this at the moment:
Uri u = imageURICamera;
getContentResolver().notifyChange(u, null);
ContentResolver cr = getContentResolver();
Bitmap bm;
try {
bm = android.provider.MediaStore.Images.Media.getBitmap(cr, u);
String path = getRealPathFromURI(u);
File f = new File(path);
When i try to query the contentProvider for the path i get the following logcat output:
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): FATAL EXCEPTION: main
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {tod.dosetracker/tod.dosetracker.ImageViewerActivity}: java.lang.NullPointerException
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.os.Looper.loop(Looper.java:123)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at java.lang.reflect.Method.invoke(Method.java:507)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at dalvik.system.NativeStart.main(Native Method)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): Caused by: java.lang.NullPointerException
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at tod.dosetracker.ImageViewerActivity.getRealPathFromURI(ImageViewerActivity.java:315)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at tod.dosetracker.ImageViewerActivity.onActivityResult(ImageViewerActivity.java:251)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.Activity.dispatchActivityResult(Activity.java:3934)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-25 20:04:04.310: ERROR/AndroidRuntime(16438): ... 11 more
07-25 20:04:04.315: ERROR/(2696): Dumpstate > /data/log/dumpstate_app_error
You already have specified this file should be saved to pic.jpg in your code
You don’t need to do anything more. You should be able to use it directly without saving in the activity result callback.
You can improve your intent launch code with a timestamp for the image.