Get File from Uri after Image Capture
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;
tr开发者_StackOverflow社区y {
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
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
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.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
String fileName = dateFormat.format(new Date()) + ".jpg";
// or use timestamp e.g String fileName = System.currentTimeMillis()+".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), fileName);
Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(cameraintent, 1);
File constructor doesn't take a Uri. You will have to get the actual file location from the MediaStore content provider, and only then you can create an instance of File for that location.
Update: The File constructor does take a Uri. But what I meant was, it takes only a Uri with a file scheme. ie. anything starting with file:// But in this case, you will always get a content uri, that starts with content://. Which doesn't really work with this constructor.
Check this thread: Get/pick an image from Android's built-in Gallery app programmatically
精彩评论