“画布:Android Studio中试图绘制太大的位图”问题

问题描述 投票:0回答:1

当我尝试从网站上获取位图时,出现“画布:试图绘制太大的问题。

所以我在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。

请告诉我。

android android-studio android-bitmap
1个回答
0
投票

错误表示对于ImageView,位图的大小太大。在setImageBitmap()到ImageView之前,您可以将较大的位图缩放为较小的位图。然后,您可以安全地将位图设置为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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.