I have a room data base with 2 entities that save images as bitmaps. But I've encountered a problem where if I try to save images that are larges(1.5MB+) which are most of the pictures taken by modern smartphones, when I try to retrieve it, the app crushes because it can only have 1MB of data.
I've tried to compress the bitmap before inserting it using https://github.com/zetbaitsu/Compressor, but it's complicated because you can't limit the size, and bitmaps are larger than what they are on files.
So I thought about 3 approaches for saving the images, an my question is which one is the best(and easy to implement), or whether there is a better approach than those.
compressing the bitmaps before saving - which is complicated and didn't work for very large files.
saving the Uri in the database instead, but if you delete the image from the phone than the image isn't saved in the database which is unexpected behavior for the user.
saving the image in storage than saving the Uri to the image - but it requires asking for permission to WRITE_EXTERNAL_STORAGE.
Thanks a lot in advance!
Related
I am working with the cards (cardView) and trying to store the images inside the array (array to string; store that in cache and when required unpack back to array). My question is: what is the most convenient way to store the images in an array?
Store the bitmaps (bitmapToString etc)? Turn the bitmaps (I get the images from the gallery and camera) into URIs? Absolute paths?
The number of images is small (3-4), so, I guess, there's no need in a DB.
Android Bitmap objects are not good candidates for storing in Java arrays - they are very large objects so you run the risk of an OutOfMemoryException. It is easy to create a memory leak if you are not careful.
If you have to get some photos from the gallery as Bitmap and retain them in your app, one of the patterns that is normally used is writing the Bitmap to the app's internal storage (covered here.
If you need a reference to the stored photo, you can save the filename or a Uri that represents the photo or image.
Then the problem of loading Bitmaps etc. into an ImageView is solved with libraries like Picasso or Glide. You can use these to load from the local file system, or from online.
I want to make an Android application that has the same look as Instagram, which means every profile has a gallery of photos.
My question is about storing and retrieve those photos. I want to know what is the best practice for photo storage in a database (MySQL), more precisely:
Should I store photos with original size (2mb for example) or resize them with smaller size?
Should I store them with two version (smaller size photo, and normal size photo)?
Saving images in the database looks like overkill, it's not needed. Saving on disk should be fine.
Although you should focus on your business logic & leave this to image libraries which does this stuff & do best for memory & performance.
Some famous libs are:
Picasso
Glide
Universal image loader
Fresco
& you will find many more
Almost each library does lots of stuff for you, such as:
In memory cache(LRU Cache) - so that UI thread is not blocked from doing I/O
operation from disks
Disk Cache - To keep images saved for further
app sessions, also when memory cache reaches its limit its have to be
evicted from it., you can specify disk cache size.
Various image scaling strategies
Image transformations
Clearing up bitmaps for freeing up memory
Network operations on background threads
Background threads management & so on..
My suggestion is to go with one of the image library
I dont think the best practice is to store the photos in a database.
In the DB you store the link/location of the photos example a url to a photo.
About smaller size photo it depends on what you are trying to accomplish because now you need more space to pay for and maybe less bandwidth because you can put thumbnails and let it only load the original size when a user clicks on it. So it all depends what and how you want to archieve
Best practice for storing images is to simply cache them as files and not save them in a database. You could save them as blobs in an sql db but they should be less than 32px by 32px to make things smooth. Everything else should be saved as files. Let me give you an example of WhatsApp.
WhatApp caches most of it's images as files and only saves the pixelated thumbnail in a database.
I am converting image as a byte array to store in the database in android. For this, I have to convert image both at the time of storing and reading. I can definitely store all image in variables during runtime at once but it might take a lot of space if there are a lot of images in the database. Does conversion actually take a lot of resources ? in that case I will use other methods to store like in file (I haven't used file before so will have to learn it first, trying to save effort :) ).
I think it depends on how many images are you planning to save in the database. For instance: if it is the user profile image, which is gonna be one image per user, I think it is okay. But, if it is something like images that the user can upload as many images as he likes, I don't really think it is a good idea. There are advantages and disadvantages on both.
There are plenty discussions about those two approaches:
Storing images in a database versus a filesystem
Why is it considered bad to store images in a database?
Storing Images in DB - Yea or Nay?
What is the best place for storing uploaded images, SQL database or disk file system?
I want to make image backup through my Application what is the best way for storing large amount of data . I tried to save image in sqlite database it worked fine for small amount of data in case of large amount it shows MemoryOutOfBoundExceptions after increasing the Heep size(android:largeHeap="true").
please help me guys .
If you want to save only image or any file try to save only path of that file in database.After that you can get file path by normal SQL query and access file from file path.
Instead of Storing Large data in Application you have to store it DataBase so it will speed up your Application and you have no Exception produce like OutOfMemoryException in your Application.
You don't store images in databases, that makes the database huge and slows it down. Store the images to disk and store the path to the file in the database. This will make the database a lot lighter and faster.
If you're developing for Android, i'd also recommend using something like Picasso, Fresco or Glide to display your images too. They cache the image and use memory efficiently when doing operations like scaling images.
im wondering if storing images as BLOB in the sqlite database would be a good idea?
Does anybody has performance - experience with storing images (blob).
My android app would be a small one, which will need to handle 20 to 100 images (100 kb up to 1MB per image). Worst case: I would say, that my database could reach a size of 100 MB. Does this has significant impact on the database performance?
Average case: I guess the average user of my app has 40 images with 200 kb per image, so the size of database would be around 8 MB.
Btw. of course the database stores also other "normal" data, so its not a image database only :)
Is storing the path to the image which is stored on the storage(internal or sd card) a better approach? I guess that retrieving the image file path from the database and open and load the image from file would be a little bit slower (but not really significant, since I need to load only two images at once).
A second question:
If I would use the second approach (store the path to image file in the database and load the image file):
Does a disk cache (DiskLruCache) is something useful in this scenario? Would it bring a significant performance boost? My understanding is that a Disk cache would store bitmaps (instead of encoded jpg or png) and therefore a disc cache would load a bitmap directly from storeage and my app would save the time to decode the image(jpg or png). Is that correct?
Btw. In the "database approach" i would store the image already decoded as bitmap. So it seems to me to be something similar as the disc cache, isnt it?
Edit:
I forgot to tell you, that i need to store the images persistent on the device. Im not talking about caching images that for example I have retrieved from a web service ...
My guess will be that the database will get significantly slower if you store that much of information in it, especially when you retrieve the images themselves.
On the other hand, as I get it, every user has the images associated with him downloaded after the application is installed. This is perfect place for using a library another SO user recommended to me in a question of mine I asked couple of days ago: Universal Image Downloader. Here is a link to the thread I speak about.
This library uses on disk caching, but abstracts away all the complexities for you (hopefully, I have not yet tried it, but it seems promising).