简体   繁体   中英

what is the best way to work with Images in SqLite android?

I am working with a customizable database with pictures. Right now I am taking pictures as it is from the sdcard and encoding it in base64 String and then putting it in the database. but whenever I am trying decoding it and showing it in my view, I am getting Out of memory error . Can any one one tell me what is the best procedure to do it? Shall I change the size of the pictures before encoding it?

I want to re-size all of the pictures into 512*512 .

Image to Base64 is very heavy operation in android. Consider saving the images on the external/internal memory and save the file path in the sqlite database.

您可以将图像转换为字节数组,然后使用BLOB类型将值存储在sql中,反之亦然。

As you mentioned you want to resize the images to 512*512, you can scale the image using below code, Create bitmap from captured image and then use below line

Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 512, 512, false);

It will give you a smaller image, you can also consider compressing the image to reduce in size,

OutputStream imagefile = new FileOutputStream("/your/file/name.jpg");
// Write 'bitmap' to file using JPEG and 50% quality hint for JPEG:
bitmap.compress(CompressFormat.JPEG, 50, imagefile);

Now, you have two options,

  1. Save the scaled and compressed image into a file and save the path of that file in db. (Better way)
  2. Convert the scaled and compressed image to base64 string and save in db.

Althought base64 is , as many answers said, a heavy operation for android, if done properly, it should not be a problem.

There are many reasons a bitmap could be required to be saved to a DB , (photo of a invoice ticket, for example?), and this is the way i do it.

first, create a new , smaller bitmap like @Swapnil commented.

and second, correctly use the bitmap transformation methods, i've been using these (look below) two so far and haven't had any memory issue on many different devices.

link to my BitmapUtils transformation methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM