Skip to content Skip to sidebar Skip to footer

"canvas: Trying To Draw Too Large Bitmap" Problem In Android Studio

When I try to get bitmap from website, Canvas: trying to draw too large problem is occured. So I search this problem on google and many people wrote their solutions, but that solut

Solution 1:

The error means the size of Bitmap is too large for the ImageView. Before setImageBitmap() to ImageView, you can scale the large Bitmap to a smaller one with Bitmap.createScaledBitmap(). Then you can safely set the Bitmap to ImageView.

A sample code:

publicclassMainActivityextendsAppCompatActivity {
    privatestaticfinalStringimageUrl="http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        finalImageViewimageView= findViewById(R.id.imageView);

        newThread(newRunnable() {
            @Overridepublicvoidrun() {
                URLurl=null;
                try {
                    url = newURL(imageUrl);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                try (BufferedInputStreambufferedInputStream=newBufferedInputStream(url.openStream())) {
                    Bitmapbitmap= BitmapFactory.decodeStream(bufferedInputStream);

                    finalBitmapscaledBitmap= Bitmap.createScaledBitmap(
                        bitmap,
                        (int) (bitmap.getWidth() * 0.1),
                        (int) (bitmap.getHeight() * 0.1),
                        true
                    );

                    runOnUiThread(newRunnable() {
                        @Overridepublicvoidrun() {
                            imageView.setImageBitmap(scaledBitmap);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

Post a Comment for ""canvas: Trying To Draw Too Large Bitmap" Problem In Android Studio"