Approaching Android Images in a DB

This is an APP with some sort of gallery context. Using REST , I get (Volley) JSON, which contains information about the place and some links to images. Because there are about 60 places, it’s not enough to cache the PICASSO form. The data is stored in the database, so I decided to include the image in the database as a byte stream . This was done, but not as good as shown below. Code inside AsyncTask .

 @Override protected Bitmap doInBackground(Void... params) { try { return Picasso.with(context) .load(site.getImageURL()) .get(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); } catch (Exception e) { e.printStackTrace(); } site.setImageBytes(stream.toByteArray()); db.openForWrite(); db.updateSite(site.getId(), site); db.close(); } 

My question is how to make it effective, do you know it is better to do it? I tried callback in Picasso to add an image to DB onSuccess , but this requires a representation as the first argument.

Perhaps there are good ways to save images to an SD card and replacing the HTTP link with the image location on the SD card.

Or direct image caching to disk?

+5
source share
7 answers

There are a few questions here:

  • onPostExecute runs in the main thread. Not a good place to work with an I / O drive. The user interface will hang from time to time when displaying images.

  • SqLite templates are faster than regular disk files, only for limited sizes ( Benchmark ). Thus, large files are better stored on disk.

  • Using Picasso is recommended if you want to cache lazy image loading. The problem is that Picasso will not allow you to easily use your custom DB as cache solution. It uses a general purpose HTTP cache. Consequently, cached files may be periodically invalidated and cleared.

If you want to fully customize the loading of HTTP and images, try something like Volley.

+3
source

I suggest you go with caching, consider using Sergey Tarasevich’s Universal Image Loader library (GitHub)

it allows detailed cache management for uploaded images, with many configurations

  • UsingFreqLimitedMemoryCache β†’ The least used bitmap is deleted when the cache size limit is exceeded.
  • LRULimitedMemoryCache: β†’ The least used bitmap is deleted when the cache size limit is exceeded.
  • FIFOLimitedMemoryCache: β†’ The FIFO rule is used to delete if the cache size limit is exceeded.
  • LargestLimitedMemoryCache: β†’ The largest bitmap is deleted when the cache size limit is exceeded.
  • LimitedAgeMemoryCache:> The Cached object is deleted when its age exceeds a certain value.
  • WeakMemoryCache: β†’ Memory cache with weak links to bitmaps.
+1
source

There is a certain mechanism for caching images in android.

  • Lrucache
  • Diskcache
  • Save to DB

If the images are not large and no more (2-3 KB), and their collection should not be expanded, you can save them in db. But it would be more difficult to write them, read them . On the other hand, the user cannot do anything with them.

Otherwise, if you want the user to be able to expand the collection of images or the images are quite large, you must save them to an SD card (Disk cache) .

Note. - I recommend using Glide library with disk caching mechanism

Glide

Consider the lack of space and properly handle these cases.

+1
source

There may be good ways to save images to an SD card and replace the HTTP Link with the location of the image on the SD card.

Or direct image caching to disk?

What I will do. Look at the cache on the SD, if the image is present, if so, use it if you do not load it into the cache and use it from the cache. But you need to take care of TTL files.

0
source

@ Biju Parvati's answer is fine, but from November 27th Universal Image Loader stopped supporting the project. You can see it on github page

I think a really good alternative is the Fresco Library . From Facebook and supports local cache storage and much more. Some of the cache properties:

Raster image cache

Coded memory cache

Disk cache

Using one disk cache or two

It is very easy to use, and image loading time is very fast. It also supports streaming progressive JPEG images over the network .

0
source

As you can see, "bitmap.compress (Bitmap.CompressFormat.JPEG, 100, stream);" The method accepts the output stream to save the bitmap image.

Just use FileOutputStream and choose a place to save the image!

0
source

Try fresco . This is a wonderful facebook library!

0
source

Source: https://habr.com/ru/post/1242111/


All Articles