0

enter image description here

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?).

After I used 'Debug App', it showed the error in more details:

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%3A1000002538 from pid=1309, uid=10925 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

Note: This is in java and I'm using Android Studio to code.

9
  • 1
    No. Where did you get that string from? It's an impossible content scheme to begin with.
    – blackapps
    Commented Jan 28, 2023 at 9:09
  • What class is Uri from? Commented Jan 28, 2023 at 12:57
  • @blackapps it is an example.. the real uri that I got is.. content://com.android.providers.media...../document/image%3A1000002538 . I'm required to increment the numbers by 1 and thus I have to convert it into a string and parse it back to uri. The problem is that the %3A is causing my string to be converted into a wrong directory for the uri.
    – Kevin
    Commented Jan 28, 2023 at 17:20
  • @JohnWilliams MainActivity class
    – Kevin
    Commented Jan 28, 2023 at 17:21
  • Do this: System.out.println(java.net.URLDecoder.decode("content://image%3A1000002539", StandardCharsets.UTF_8.name())); . Note that the %3A becomes a : - not sure that fixes the problem Commented Jan 28, 2023 at 18:27

1 Answer 1

1

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, %3A represents the colon character (:). So the unencoded "opaque" component of that URI is actually

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

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

I'm not entirely sure why your "string bashing" approach is failing, but 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();

By calling getSchemeSpecificPart() we are getting the relevant part of the URI with the percent encoding decoded. Likewise, the Builder is going to re-apply encoding as required.

CAVEATS

  1. This code is not compiled or tested. I don't have an Android dev platform.
  2. For non-Android folks, this is using the Android Uri class not the Java SE URI class!
8
  • I tried it and it didn't work even for setText().. The error showed: java.lang.AssertionError
    – Kevin
    Commented Jan 29, 2023 at 16:34
  • Its a good answer. However, there seems to be an error at this line: assert parts.length == 1 && "image".equals(parts[0]);
    – Kevin
    Commented Jan 29, 2023 at 16:43
  • So ... take a look at what is in the elements of parts. Like I said, I can't debug the code. (I assume that you understand the purpose of an assert statement ...)
    – Stephen C
    Commented Jan 29, 2023 at 22:21
  • 1
    Hint: add some traceprinting or logging before the assert to show you the information you need to look at. Or use a debugger, single step it through the relevant parts of the code and examine the variables. (This is a Q&A site for programmers. It is assumed that you either know how to do these things already, or can self-educate on how to do them.)
    – Stephen C
    Commented Jan 30, 2023 at 0:29
  • 2
    OK ... so that explains why I couldn't figure out why your original code was failing. It wasn't! However the thing I pointed out about your assumption about the number length IS true. And that could translate into errors in ... one in one thousand cases.
    – Stephen C
    Commented Jan 30, 2023 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.