-1

I'm using Android-Universal-Image-Loader for downloading image files from my server using .loadImageSync(imageURL) method.

And then I need to save that bitmap to user device external storage.

File file = new File(mContext.getExternalFilesDir(null) + directoryIntermediatePath, fileName); FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

fileOutputStream.flush();

fileOutputStream.close();

My issue is .png file on my server holding size of approx 200KB, which will become approx 700KB after this process on android device storage.

Community
  • 1
  • 1
Manish Jain
  • 842
  • 1
  • 11
  • 29
  • instead of compressing with quality to 100 change it to 70 or 80 which will reduce your size. – SilentKiller Sep 20 '16 at 11:04
  • But that way I can not figure out what file size exactly will be on user device, I want to be sure that quality and size will be the same as on my server. – Manish Jain Sep 20 '16 at 11:09
  • @SilentKiller since PNG uses a lossless compression I doubt that reducing the quality has any effect. – Henry Sep 20 '16 at 11:15
  • @ManishJain There are only 2 ways to reduce the size of image One is to reduce the quality but as Henry said PNG uses a lossless compression so this one will not effect and another way is that you should reduce(scale) the size(height,width) of the image and then store it to sd card. – SilentKiller Sep 20 '16 at 11:18
  • The Image size size also depends on the width and height. scale your bitmap down,before saving it to Android. This will help. – HourGlass Sep 20 '16 at 11:19
  • If you first make a Bitmap of a png file and then try to make a png file from that bitmap you will mostly end with different filesizes. Especially when you use libraries. If you want the original file then just download the png file and do not convert it to Bitmap at all. But... you did not show at all where you get that Bitmap from. You should have told that of course. – greenapps Sep 20 '16 at 11:24
  • AFAIK UIL is not meant to simply download Image files. Its geared for asynchronous display without need to manage files yourself. If it's about [caching then use UIL caching, otherwise download the images yourself](http://stackoverflow.com/a/16019103/3828957). Maybe using another Loader Library. – makadev Sep 20 '16 at 11:30

2 Answers2

0

You should not save the loaded image to disk. Instead you should use the file as it had been downloaded.

If I got it right the Android-Universal-Image-Loader library uses a disk cache.

If you set a specific DiskCache implementation while building the ImageLoaderConfiguration you should be able to access this DiskCache later and retrieve it later via:

File f = DiskCacheUtils.findInCache(imageURL, diskCache);

Then you only have to copy the file on byte[] level.

Robert
  • 39,162
  • 17
  • 99
  • 152
-1

You can try reducing the quality parameter in bitmap.compress to some value less than 100. However, for PNG image, which is a lossless format, Bitmap will ignore the quality parameter. So, an option is to convert to JPEG for reduced size. For example, use this line:

bitmap.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);

For more information on bitmap.compress, check here.

If you want to reduce to a specific size using PNG, you'll have to re-scale your image; this SO link may prove useful.

Community
  • 1
  • 1
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47
  • Question is not at all about .jpeg file, it is abut .png file. And another thing is that it is not about reducing the file size, it is about maintain the same size which file actually hold when it is downloaded from server. – Manish Jain Sep 21 '16 at 06:17
  • @ManishJain, using JPEG is just an option in order to reduce the file size. It isn't clear in the question that the stored image should be of the same format. If you would prefer to keep the image as a PNG then the mentioned link in my answer will be helpful. Hope this helps. – Divyanshu Maithani Sep 21 '16 at 07:41