"Canvas: trying to draw too large bitmap" Android Studio 中的问题

"Canvas: trying to draw too large bitmap" problem in Android Studio

当我尝试从网站获取 bitmap 时,出现 Canvas: trying to draw too large 问题。

所以我在 google 上搜索了这个问题,很多人写了他们的解决方案,但那个解决方案是关于 drawable 目录中的位图文件。

这是我的代码。

            Thread mThread = new Thread() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(postImgUrl);

                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true);
                        conn.connect();

                        InputStream inputStream = conn.getInputStream();
                        postImgBitmap = BitmapFactory.decodeStream(inputStream);
                        inputStream.close();
                        conn.disconnect();

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            mThread.start();
            mThread.join();

            if (postImgBitmap != null){
                postImg.setImageBitmap(postImgBitmap);

出现问题时,变量postImgUrl为“http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg”,变量postImg为ImageView。

请多多指教

该错误表示 Bitmap 的大小对于 ImageView 来说太大了。 在 setImageBitmap() 到 ImageView 之前,您可以使用 Bitmap.createScaledBitmap() 将大位图缩放到较小的位图。然后你就可以安全地将Bitmap设置为ImageView了。

示例代码:

public class MainActivity extends AppCompatActivity {
    private static final String imageUrl
        = "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView imageView = findViewById(R.id.imageView);

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                try {
                    url = new URL(imageUrl);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                try (BufferedInputStream bufferedInputStream
                        = new BufferedInputStream(url.openStream())) {
                    Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);

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

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(scaledBitmap);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}