stephenduan
stephenduan

Reputation: 1

Android display larger image

I am trying to display a larger image,but it throw OutOfMemoryException. How to display a larger image, without OutOfMemoryException, don't reduce the picture quality at the same time?? display in ImageView.

Upvotes: 0

Views: 49

Answers (2)

M D
M D

Reputation: 47807

If image size is too large. Need to scale as below code.

      private Bitmap myBitmap;
      try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                myBitmap = "img_path";

                if (options.outWidth > 3000 || options.outHeight > 2000) {
                    options.inSampleSize = 4;
                } else if (options.outWidth > 2000 || options.outHeight > 1500) {
                    options.inSampleSize = 3;
                } else if (options.outWidth > 1000 || options.outHeight > 1000) {
                    options.inSampleSize = 2;
                }
                options.inJustDecodeBounds = false;

                if (myBitmap != null) {
                    try {
                        if (imageView != null) {
                            imageView.setImageBitmap(myBitmap);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                System.gc();
            }

For more information go to official Loading Large Bitmaps Efficiently

Upvotes: 1

luiscosta
luiscosta

Reputation: 855

Maybe if you divide the larger picture into smaller ones: How to split an image into multiple smaller images

And then put them all together with the help of the standard Layouts of Android.

This is just an idea, I didn't actually tested it or anything.

Hope this helps.

Upvotes: 0

Related Questions