My goal is to:
- Save media file to External Storage (in my case it's photo).
- Get file path or URI of saved data.
- Save it to SQLite (either file path or content URI or smth else).
- Be able to get correct URI to this content at any point in the future.
It's very similar to what other very popular application do - they create their directory in 'Pictures' folder and store there photos and use them in their applications while they're also available for viewing using gallery/file explorer etc.
As I understand recommended way to save media content (image, f.e.) is to use MediaStore
API and as a result I get content URI, which I can use later. But then I read that these content URIs might be changed after re-scan of Media happens, so it looks it's not reliable. (For example if SD card is used and it's taken out and inserted again)
At the same time usage of absolute file paths is not recommended and there's tendency to deprecate APIs which use absolute file paths to work with External Storage. So it doesn't look reliable either.
I can only imagine the following solution:
- Use unique auto-generated file name while saving (like UUID).
- When I need to get content URI (f.e. want to render photo in
ImageView
) - I can useContentResolver
and search for content URI using file name filter.
Problem with this approach is that I have a lot of photos (gallery) and querying it using ContentResolver
can affect performance significantly.
I feel like I'm over complicating things and missing something.
from Saving media file path or URI to SQLite and getting it, best practice
No comments:
Post a Comment