简体   繁体   中英

Is there any efficient way in Android to downscale large image from file and save it to new file without OOM exception

I am frustrating very long time on this problem. I have special case: I need resize (downscale) large existing image that stored in file and save it to new file. I do not show this image in image view, so techniques like use Glide, Picasso or options.inJustDecodeBounds = true will not help in this case. For example I have 8k image and I need to downscale it to 4K and then save to new file. On low end devices I will most likely see OOM exception when I will use options.inJustDecodeBounds = true , calculate inSampleSize and then call BitmapFactory.decodeResource(res, resId, options) because it will try to allocate memory for 4K bitmap (result bitmap will be stored in memory before save to disk). Any suggestions?

There is a class in the Android framework that can be used to decode only small regions of an image at a time: BitmapRegionDecoder . As @emandt mentioned, resizing it this way may give you some undesirable results, although you could experiment with picking different region sizes (perhaps some multiple of the rescale percentage could improve the results). Depending on your requirements these artifacts may not be a problem.

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