'Dealing with Android App Images - Store in Room as Bytes or URI?

I want to add the ability for my Android app to handle images the user may select to go with their other data. The app has been "upgraded" to use the Room implementation.

Say right now I have a "Widget" model which contains "Name" and "Notes" fields. The user enters data for those in normal textboxes within the app, but I'd also like to add an ImagePicker where they can add several images to this "Widget" item.

I decided to go with storing the images in the database (for reason I explain below) and I've already got the database table and model updates for it and I believe it is working, but I saw the database is 123 MB even though it had a single < 5 MB image in it, as far as I know.

Anyway, I guess the better solution than storing the image bytes in the app database is to store the image URI, but what cautioned me about that in the first place was how easily images can be moved around in a phone. People may move them to different folders (albums) very easily and then that image is broken in the app.

I was just wondering if anyone else had worked on an app that stored user-selected images with other app data. Perhaps when the image is chosen, I could make a copy of it to my app's folder? Probably at the same time I'd resize the image if it's huge, because there's no point in keeping some 4K image when it's just going to be displayed in the app as a thumbnail (maybe clicking the image will open it up full screen, but still). However I'm not sure taking up more storage space on a user's device (without them really knowing) is so great. What say you, haxors?



Solution 1:[1]

  1. You should not save images URIs because URIs are temporary. Use the path of the file instead. Big data sizes in the database can be dangerous. If the file is already saved, I suggest you use path. When using those images, add a null check to prevent crashes. At least your database will not crash because you saved path strings only.

  2. You can use Base64 conversion to convert image files to string files. You can save encoded strings in the database

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alperen Acikgoz