1

When I launch a Photo Capture intent, the photo path that is gave to me in return is : content://media/external/images/media/40209 but when I look in my device, the photo path should have been something like [..]/pictures/1456164469539.jpg

Do you know how to get the second path from the first ?

note I use the method described there Android ACTION_IMAGE_CAPTURE Intent by yanokwa.

Thanks,

-------------------- EDIT

I launch my intent like so :

private void launchPhotoIntent() {
    Uri photoUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new ContentValues());
    Log.i("renaud","photoUri : "+mPhotoUri.toString());
    SharedPreferences sharedPreferences = getActivity().getSharedPreferences(AppConstants.SP,Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("test",photoUri.toString()).commit();
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    getActivity().startActivityForResult(intent, ACTION_TAKE_PHOTO);
}

and In my Callback :

else if(requestCode == PostMessageWindowFragment.ACTION_TAKE_PHOTO && resultCode == Activity.RESULT_OK){
    Uri uri = Uri.parse(sharedPreferences.getString("test",null)); // data.getData();
    File file = new File(event.uri.getPath());
    Log.i("PICTEST",""+file.length());
}

It logs "0"

Community
  • 1
  • 1
Renaud Favier
  • 1,645
  • 1
  • 17
  • 33

2 Answers2

2

When I launch a Photo Capture intent, the photo path that is gave to me in return is : content://media/external/images/media/40209

That is not a path. That is a Uri, pointing to content.

the photo path should have been something like [..]/pictures/1456164469539.jpg

Not necessarily.

First, there are thousands of Android device models and thousands of camera apps (both pre-installed and installed by users), some of which implement ACTION_IMAGE_CAPTURE. What one camera app does will not necessarily match what another camera app does.

Second, if you read the documentation for ACTION_IMAGE_CAPTURE, you will notice that there is no "photo path that is gave to me in return". If you supply EXTRA_OUTPUT, your photo should be in that location. If you do not, use getExtra("data") on the Intent passed to onActivityResult() to get a thumbnail bitmap. You appear to be assuming that the Intent will have a Uri, and few camera apps do that.

Do you know how to get the second path from the first ?

That is not possible in general, as a Uri does not have to point to a file, let alone a file that you can access. Use ContentResovler to work with Uri values, such as openInputStream() to read in the content pointed to by a Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for such a detailed answed. After getting this image, I'm uploading it to a server throught a multipart request. I already did it for files, it works well. So I tried to build a file with ```final File file = new File(uri.getPath());``` passing the returned Uri as parameter (it created an empty file). But from what I understand, I should have created a file with a random chosen path, and then fill it throught InputStream ? – Renaud Favier Feb 23 '16 at 13:31
  • @RenaudFavier: "I should have created a file with a random chosen path" -- if you are using `ACTION_IMAGE_CAPTURE`, and you want a full-size photo, you need to use `EXTRA_OUTPUT`, supplying in there a `Uri` that points to where you want the full-size photo to be. In practice, a `Uri` pointing to a location on [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) should work fine. Then, you can use a `File` object pointing to that location for your upload. – CommonsWare Feb 23 '16 at 13:37
  • Humm that's what I first did, but It didn't work then. I'm working on a nexus 5 with android 6, and red that it doens't work as expected on this device. Do you confirm this ? Anyway I'll explore further more, surely I have been doing something wrong. Thank you for your help. I'll come back later if I'm still stuck and update my question otherwise – Renaud Favier Feb 23 '16 at 13:43
  • @RenaudFavier: `ACTION_IMAGE_CAPTURE` relies on third-party camera apps, which can have bugs. Only use `ACTION_IMAGE_CAPTURE` if taking a photo is not particularly important to you or the user. Otherwise, you will need to integrate with the camera APIs directly. – CommonsWare Feb 23 '16 at 13:49
  • I updated my question with the code I used with your suggestions. It's still making an empty file, my camra app is the google default one, I'm a bit surprised it's bugged. I think I'll follow your advice and directly deal with the camera API. – Renaud Favier Feb 23 '16 at 14:04
  • @RenaudFavier: That library is designed to be an `ACTION_IMAGE_CAPTURE` and `ACTION_VIDEO_CAPTURE` replacement. It is still a work in progress, but you are welcome to experiment with it and see how it compares to your needs. – CommonsWare Feb 23 '16 at 14:25
  • I'll give it a try. Thank you for your help. – Renaud Favier Feb 23 '16 at 14:27
0

The path you get is the file URI. Try with converting it to path using this Convert file: Uri to File in Android

Community
  • 1
  • 1
Boban S.
  • 1,662
  • 13
  • 16
  • I don't think that's what I'm looking for, I know well how to convert uri to file. But the first thing is not the right uri, this creates me an empty file. – Renaud Favier Feb 23 '16 at 11:38
  • Unfortunatly, there is a known bug (see the link in my post), that makes the documented way of taking a photo not work on my nexus 5. – Renaud Favier Feb 23 '16 at 13:26