Can anyone explain how to store image from our gal...
# android
k
Can anyone explain how to store image from our gallery to room database?
👀 1
😶 3
google 1
🚫 1
u
You have two options 1. Store the uri as text 2. Store image bytes array as blob
k
If I store uri as string and then cast string as uri won't it work ?
u
You need to extract path from uri and then store it And when you fetch so you need to create content uri from that path
j
I think its not a good idea storing images to room, you can simply dump the images to local folder. Like sharedpreferrences do.
k
I have done like that but there was an error that can not convert string to uri and app crashed
j
not exactly converting string to uri but save images in local folder as bitmap.
u
You need the uri anyways Otherwise how you will distinguish and fetch particular image It's all about to have a unique reference for each image resource @Johnjake Talledo
j
I simply store the image details into room like image id or anything that identify the particular image. Also the name of the image
this_is_first_image.jpg
so when I need to retrieved a particular image I will query first the local database or room, as room contains the
image_location
,
image_name
,
image_file_extension
and
image_id
.
k
Can you provide code or github link , so I can understand it better
j
When I need to save the image in local folder I am also inserting the image details in room, as this are my bases of identifying particular image.
k
So , when I select image from gallery and store in room database then detailes will be stored automatically
j
I will look first on my repo as I forgot the project name, the idea is save image in local folder and save the image information in room. This is how I cache the avatar image or profile image of each user.
k
Yes, I want to store image exactly for same purpose as you
When I upload image without storing and reopen my app, image is vanished
j
Yes because you are calling two function or method one is save image as bitmap and the other one is save image details in room.
So when you retrieved image you will first
select from room image_id = 1
it will give you image name and image location.
k
Whenever you are free, please provide me github link of that project so I can visualize better
👍 1
803 Views