Home > Enterprise >  Use string url or URI for image properties of object
Use string url or URI for image properties of object

Time:09-29

I want to store object to data room. when i create an object like account with name, phone number, email and images taken from gallery. Should I use string or uri for images so that objects can be easily passed between activities?

CodePudding user response:

I think better to store this image as object BLOB otherwise if you removed this image from gallery it will not be able to get this, because Room, will stored only link not an object. I hope it will be useful

@Entity(tableName = "user")
public class User{

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
}

CodePudding user response:

You should copy images to the app's local storage and store that copied image path in the database.

For copying the image to the app's local storage after getting the selected image uri

val dir = requireActivity().getDir(FOLDER_TO_SAVE, Context.MODE_PRIVATE).path
val copiedImage = File(dir, UUID.randomUUID().toString()   ".jpg")
val copiedImageUri = Uri.fromFile(file)

receivedImageUri.path?.let {
    File(it).copyTo(copiedImage)
}

Get path of the copied image, using its uri

copiedImageUri.path.?.let{
     val copiedImagePath = "file://$it"
     //now save this path to your database
}

Use this saved image path as string to exchange between activities.

  • Related