2

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
Arcadia
  • 266
  • 4
  • 17

2 Answers2

5

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);
Moog
  • 10,193
  • 2
  • 40
  • 66
0

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

Community
  • 1
  • 1
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • http://developer.android.com/reference/java/io/File.html Yes it takes URI. But the your solution is good. – Nikola Despotoski Jul 25 '11 at 17:51
  • 1
    The doc says: "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." So, you need a file:// scheme here. I will update my answer and make it more clear. – Kumar Bibek Jul 25 '11 at 17:53
  • 1
    Try and debug and check what uri are you getting, and what happens when you query the Content Provider for the path, and post the results. – Kumar Bibek Jul 25 '11 at 17:59
  • Is your picture really getting saved? Check the sdcard. I don't think you can create a file at the root of your sdcard. If the picture is getting saved, then you don't have to query for the path, because you already know what is the path of your image. – Kumar Bibek Jul 25 '11 at 18:11
  • Yes the file is getting saved.. after program crash I can go to gallery and find it there – Arcadia Jul 25 '11 at 18:14
  • @KumarBibek let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1805/discussion-between-arcadia-and-kumar-bibek) – Arcadia Jul 25 '11 at 18:14