1

I am having problems converting a uri to a path because of the content tag. So I am trying to select any file type from the Android storage, and I am able to select a file but when I grab the data and try to convert it to a string path my app crashes.

My code to convert the uri looks like

String path = data.getData().getPath();

I've looked around and some say to use a Content provider and content resolver, but I'm not sure how to use them. Any help would be great thanks.

Display it/ upload it to an s3 bucket. When I mean display it, I mean if it's a photo, or video to show it and if it's an audio file I'd like to be able to play it and the same with other files, like PDF and so on.

k3j123
  • 13
  • 6
  • what are you trying to do with the file? why do you need the path? – LordRaydenMK Dec 26 '17 at 18:57
  • Display it/ upload it to an s3 bucket. When I mean display it, I mean if it's a photo, or video to show it and if it's an audio file I'd like to be able to play it and the same with other files, like PDF and so on. – k3j123 Dec 26 '17 at 18:58

1 Answers1

2

This method returns the Path as String

private String getPath(Uri uri) {

            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null,null);

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            return cursor.getString(column_index);
        }
Mohamed Embaby
  • 960
  • 8
  • 26
  • Using the above code I am getting a runtime exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:206 flg=0x43 }} – k3j123 Dec 26 '17 at 23:49
  • MinSdk is 21 and TargetSdk is 26. The uri right now is for local storage, but I would like the option for both. – k3j123 Dec 27 '17 at 00:50
  • This is what the uri that is returned when I log it. content://com.android.providers.media.documents/document/image:206 – k3j123 Dec 27 '17 at 00:56
  • hard to relate, the above method works fine and tested. though, there's two type of data returned from ACTION_GET_CONTENT see [link](https://stackoverflow.com/q/20067508/7569106) I tested on content://media/external/images/media/3303 not your type maybe that's the reason. but that also will not throw a RuntimeException. if you could edit your question for more code would be better – Mohamed Embaby Dec 27 '17 at 01:45