Search code examples
androidpicasso

Picasso Android don't load Uri of type "content://"


I'm working on a android app which handles pictures.

On the MainActivity, you can choose to take a picture with the camera or a picture from your phone. Then, after some processing, the app displays an activity with some results and the picture which is loaded by Picasso with: pic.load(uri).resize(1000, 1000).centerCrop().into(normalImage);

During the process, I'm saving my URI's and results in a SQLite database. Everything works fine here.

On app's restart, everything is loaded but pictures which were taken from the phone. Pictures from the camera are ok.

Pictures from camera have a "file://" type URI. Pictures from phone have a "content://" type URI.

I don't know which code part I can post to help you, don't hesitate to tell me!

Thanks guys.


Solution

  • During the process, I'm saving my URI's and results in a SQLite database. Everything works fine here.

    Not really.

    For any Uri that you receive, you should assume that you only have temporary access to its contents. This will be especially true with content: Uri values. Persisting the Uri is pointless, as you may not be able to use the value in the future, particularly after your process has been terminated.

    Treat a Uri like a URL to a Web resource. Some URLs are publicly accessible, and those could be persisted and used later. Other URLs might work so long as the user is logged in, but once the user's session times out, the URL is useless. The same basically holds true for Uri values.

    Your choices are:

    • On Android 4.4+, replace ACTION_GET_CONTENT with ACTION_OPEN_DOCUMENT, then use takePersistableUriPermission() to try to get long-term access to the Uri, as part of the Storage Access Framework

    • Make a local copy of the content in your app, with suitable adjustments to your UI terminology so that the user understands that this is what is happening (e.g., "Copy", "Attach", "Import")