Search code examples
androidthumbnailsandroid-image

How to save images in .thumbnail folder with proper format?


I need to download images from server and store as thumbnails directly. and for display images also i need to use images from .thumbnails folder directly. i am not getting how to create and save images as .thumbnail . and how to use images from that .thumbnail file.i searched online but everywhere only this below code present.

Bitmap thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);

any help?


Solution

  • Please check below code which helps you.

    Follow below steps:

    1. Calculate the maximum possible inSampleSize that still yields an image larger than your target.
    2. Load the image using BitmapFactory.decodeFile(file, options), passing inSampleSize as an option.
    3. Resize to the desired dimensions using Bitmap.createScaledBitmap(). Now you have your bitmap ready and you can save it any where using the following code

       Bitmap thumbnail;
       File thumbnailFile = ...;
       FileOutputStream fos = new FileOutputStream(thumbnailFile);
       thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
       fos.flush();
       fos.close();
      

    hope it helps you and save your time.

    Or use Glide library which store image as a full image and 1 thmbnail image which is power caching library which loads image quickly and once download image after load image from cache not from network. Below is a Glide link please read it advantages and features and after used into your applications.

    https://github.com/bumptech/glide