Search code examples
androidandroid-intentandroid-camera-intent

Photo intent, don't understand the path


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"


Solution

  • 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.