Home > Mobile >  How to convert String to Uri if it has % in it?
How to convert String to Uri if it has % in it?

Time:01-29

enter image description hereI have problems converting this string "content://com.android.providers.media.documents/document/image:1000002538" into a Uri correctly.

The Situation:

I decremented a Uri

  • First, I converted the Uri into a string and in turn into an int

    Afterwhich, I did a -1, and then I got the string that looks exactly like a decremented string

    However, when I parse the uri and try to setImageURI() on it,

    it is showing "File error accessing recents directory (directory
    doesn't exist?)
    ."

Here is the code that I have used:

            Uri ImageUri = data.getData();
            String uri1 = ImageUri.toString();

            //region uri2
            String substr1 = uri1.substring(uri1.length()-3);
            int substr1int = parseInt(substr1)-1;
            String decrementedstr1 = new Integer(substr1int).toString();
            int numberofchars1 = uri1.length()-3;
            String firstcomponent1 = uri1.substring(0, numberofchars1);
            String uri2 = firstcomponent1   decrementedstr1;


            //endregion
            Uri test = Uri.parse(uri2);
            animateobject.setImageURI(test);

Got this Error:

File error accessing recents directory (directory doesn't exist?).

CodePudding user response:

Try to use toString() method to convert into the string.

CodePudding user response:

Let's first get something straight. What is the meaning of that % character?

Well ... if you look at the URI Specification (RFC ....) the % is a percent encoding marker. The two characters after the % are hex digits, and the whole thing represents an ASCII character. In fact, : represents the colon character (:). So the unencoded path component of that URI is actually

content://com.android.providers.media.documents/document/image:1000002538

So the image (document) number is really 1000002538 and decrementing it should presumably give 1000002537 as the image number.

I think that your version of the code is failing because you are (in effect) double encoding the %. That results in the "consumer" of this URI seeing a garbled path. Also, you are decrementing just the last 3 digits of the image numbers ... and your example has 4 significant digits on the right end.

So here's how you should code it:

        Uri imageUri = data.getData();
        String[] pathSegments = imageUri.getSchemeSpecificPart().split("/");
        String lastSegment = pathSegments[pathSegmentslength - 1);

        String[] parts = lastSegment.split(":");
        assert parts.length == 1 && "image".equals(parts[0]);
        long imageNo = Long.parseLong(parts[1]);
        imageNo--;
        lastSegment = "image:"   imageNo;

        pathSegments[pathSegments.length - 1] = lastSegment;
        String path = String.join("/", pathSegments);
        imageUri = Uri.Builder().scheme("content").opaquePart(path).build();

CAVEAT - this code is not compiled or tested. I don't have an Android dev platform.

  • Related