2

I get an Uri and I do:

String s = Uri.decode(uri.toString());

But when I encode the string again I haven't got the same result:

Uri uri = Uri.parse(Uri.encode(s));

Uri received without decoding (just called toString()):

content://com.android.externalstorage.documents/document/primary%3APictures%2FScreenshots%2FScreenshot_20190807-153556.png

Uri created via parse/encode method:

content%3A%2F%2Fcom.android.externalstorage.documents%2Fdocument%2Fprimary%3APictures%2FScreenshots%2FScreenshot_20190807-153556.png

Is there a way to re-parse correctly the Uri?

2
  • 1
    can't you just skip encoding/decoding? that string has nothing which would need to be encoded, eg %20. for filenames with a space .replace() would not encode too much. Commented Sep 7, 2019 at 7:47
  • No because I need to show the decoded string Commented Sep 7, 2019 at 7:48

2 Answers 2

1

Is there a way to re-parse correctly the Uri?

No. Hold onto the original Uri. This is not significantly different than converting an image to monochrome, then wondering how to get the original color image back. The decode() and encode() methods are not designed to decode and encode Uri values, but rather specific pieces (e.g., query parameter values).

Sign up to request clarification or add additional context in comments.

Comments

0

URLEncoder.encode and URLDecoder.decode did the job for me:

    val uri1 = "content://media/external/images/media/1000000076"
    Timber.v("uri1=$uri1")
    val str = URLEncoder.encode(uri1, StandardCharsets.UTF_8.toString())
    Timber.v("str=$str")
    val uri2 = URLDecoder.decode(str, StandardCharsets.UTF_8.toString())
    Timber.v("uri2=$uri2")

I get following output:

uri1=content://media/external/images/media/1000000076
str=content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F1000000076
uri2=content://media/external/images/media/1000000076

Also worked for original poster string:

uri1=content://com.android.externalstorage.documents/document/primary%3APictures%2FScreenshots%2FScreenshot_20190807-153556.png
str=content%3A%2F%2Fcom.android.externalstorage.documents%2Fdocument%2Fprimary%253APictures%252FScreenshots%252FScreenshot_20190807-153556.png
uri2=content://com.android.externalstorage.documents/document/primary%3APictures%2FScreenshots%2FScreenshot_20190807-153556.png

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.