Android: universal image downloader Download large images

I need to upload a large image with dimensions of 2450 x 2450 pixels.

Bitmap bitmap = ImageLoader.getInstance().loadImageSync(url, ImageConfig.getImageOptions()); 

The problem is that an exception was received from the memory on a low-level device (a phone with RAM less than 1 GB).

This is my DisplayImageOptions

 public static DisplayImageOptions getImageOptions() { DisplayImageOptions.Builder options = new DisplayImageOptions.Builder(); options.delayBeforeLoading(10) //The reason I'm using ARGB_8888 because I need to load bitmap without losing it quality .bitmapConfig(Bitmap.Config.ARGB_8888) .imageScaleType(ImageScaleType.NONE) //I'm not using disk or memory cache .cacheOnDisk(false) .cacheInMemory(false); return options.build(); } 

I tried to add the target size based on the resolution of the device, but it does not work, the downloaded bitmap still has dimensions of 2450 x 2450 pixels.

 int height = orientation == Configuration.ORIENTATION_PORTRAIT ? displaymetrics.heightPixels : displaymetrics.widthPixels; Bitmap bitmap = ImageLoader.getInstance().loadImageSync(imageUri, new ImageSize(height, height), ImageConfig.getImageOptions()); 

I tried to change imageScaleType to ImageScaleType.EXACTLY by downloading a bitmap with a size of up to 1225 x 1225 pixels on all devices, I need to get the original dimensions for a high-performance device and resize for a low-level device.

The main problem is the size of the target does not work.

Any idea how to upload an image, especially for a low-maintenance device?

+5
source share
2 answers

try using some library like picasso .

It is very easy to use and manages resizing for you.

If you want to do it yourself, I suggest you do it using the NDK in C ++, because there you have better memory management and you can help with low-level devices.

+1
source

Just change imageScaleType to ImageScaleType.EXACTLY_STRETCHED will fix the problem. The size of the target will work as intended. Stangely UIL will try to maintain the aspect ratio based on the highest value between the target width and the target height.

DisplayImageOptions

 public static DisplayImageOptions getImageOptions() { DisplayImageOptions.Builder options = new DisplayImageOptions.Builder(); options.delayBeforeLoading(10) //The reason I'm using ARGB_8888 because I need to load bitmap without losing it quality .bitmapConfig(Bitmap.Config.ARGB_8888) .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) //I'm not using disk or memory cache .cacheOnDisk(false) .cacheInMemory(false); return options.build(); } 

Set target size based on device size when loading a bitmap

 //In this case the device I'm using for testing has 2392 pixels height int height = orientation == Configuration.ORIENTATION_PORTRAIT ? displaymetrics.heightPixels : displaymetrics.widthPixels; //In this case the device I'm using for testing has 1440 pixels width int width = orientation == Configuration.ORIENTATION_PORTRAIT ? displaymetrics.widthPixels : displaymetrics.heightPixels; //Loaded bitmap will has 2392 x 2392 pixels dimensions Bitmap bitmap = ImageLoader.getInstance().loadImageSync(imageUri, new ImageSize(width, height), ImageConfig.getImageOptions()); 
+1
source

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


All Articles