Insufficient image loading imageViews

I have a scrollview with small thumbnails of images uploaded via AsyncTask, and it outputs the URL of the image to the image.

They are added dynamically, and then on top, this is the main image that holds the image of the thumbnail that you clicked.

Everything works fine until you have more than 10 images in the thumbnails ...

I load the mainImage url just like thumbnails, so when they click on the image in the thumb, it loads it up.

I recycle the bitmap in the method itself, but it seems to run out of memory and crash when loading more than 10 images (thumbnails load normally, but crash when clicking the button to load the main image)

any help appreciated

this is the code i use to upload images (thumbnails + main):

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } 
+4
source share
8 answers

just implement this on ur image ... it will reduce ur image 4 times

 public static Bitmap getImage(byte[] image) { BitmapFactory.Options config = new BitmapFactory.Options(); config.inPreferredConfig = Bitmap.Config.RGB_565; config.inSampleSize = 4; return BitmapFactory.decodeByteArray(image, 0, image.length,config); } 
+1
source

You should read the next two official Android tutorials. They will teach you how to efficiently download large bitmaps, and they provide working code samples that we use in our production applications.

Effective display of raster images

Efficiently load large raster images

+1
source

You do not close the input stream. This way, all the time when images access created input stream objects, which are expensive and raise an OutofMemoryError. add below code after catch block.

 in.close(); in = null; 
+1
source

You probably have to work with some kind of cache strategy for bitmaps, try looking at the Universal Image Loader library, it works very well, what you need

Disposal of raster images yourself should only be done when you know for sure that it will no longer be used.

0
source

Try resizing the bitmap to a smaller size (for example, example 1.5: width = widt * 1.5) then strech (fit xy or something else) in your view. This will reduce image quality due to a factor that you decide. this is a quick dirty way. also makes rgb bitmap, as mentioned by Niun Goia, works well.

0
source

You should not use ScrollView for this. You must use ListView. When you use ScrollView (with LinearLayout or something like this inside it), all the items in the list (no matter how important) will try to load right away. Try using a ListView instead (which is precisely why this is done; it only loads ui elements from visible lines into memory).

0
source

You can use android: largeHeap = "true" to request a larger heap size

answer

0
source

I am using this.

 Options opt = new Options(); opt.inPurgeable = true; opt.inInputShareable = true; opt.inPreferredConfig = Bitmap.Config.RGB_565; 
0
source

All Articles