“Canvas: trying to draw too large bitmap” 在 Android Studio 中的问题。

huangapple go评论154阅读模式
英文:

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

问题

从网站获取bitmap时,出现了“Canvas: trying to draw too large”问题。

因此,我在谷歌上搜索了这个问题,许多人写了他们的解决方案,但这些解决方案是关于目录drawable中的位图文件的。

  • 如果从网站获取的位图图像太大怎么办?

这是我的代码。

  1. Thread mThread = new Thread() {
  2. @Override
  3. public void run() {
  4. try {
  5. URL url = new URL(postImgUrl);
  6. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  7. conn.setDoInput(true);
  8. conn.connect();
  9. InputStream inputStream = conn.getInputStream();
  10. postImgBitmap = BitmapFactory.decodeStream(inputStream);
  11. inputStream.close();
  12. conn.disconnect();
  13. } catch (MalformedURLException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. };
  20. mThread.start();
  21. mThread.join();
  22. if (postImgBitmap != null){
  23. postImg.setImageBitmap(postImgBitmap);

当问题出现时,变量postImgUrl为"http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg",变量postImgImageView

请给我建议。

英文:

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 solution is about bitmap file in directory drawable.

  • What should I do if the bitmap image taken from the website is too large?

This is my code.

  1. Thread mThread = new Thread() {
  2. @Override
  3. public void run() {
  4. try {
  5. URL url = new URL(postImgUrl);
  6. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  7. conn.setDoInput(true);
  8. conn.connect();
  9. InputStream inputStream = conn.getInputStream();
  10. postImgBitmap = BitmapFactory.decodeStream(inputStream);
  11. inputStream.close();
  12. conn.disconnect();
  13. } catch (MalformedURLException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. };
  20. mThread.start();
  21. mThread.join();
  22. if (postImgBitmap != null){
  23. postImg.setImageBitmap(postImgBitmap);

When a problem occurs, variable postImgUrl is "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg" and variable postImg is ImageView.

Please advise me.

答案1

得分: 0

以下是翻译好的内容:

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

示例代码:

  1. public class MainActivity extends AppCompatActivity {
  2. private static final String imageUrl
  3. = "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. final ImageView imageView = findViewById(R.id.imageView);
  9. new Thread(new Runnable() {
  10. @Override
  11. public void run() {
  12. URL url = null;
  13. try {
  14. url = new URL(imageUrl);
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. }
  18. try (BufferedInputStream bufferedInputStream
  19. = new BufferedInputStream(url.openStream())) {
  20. Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
  21. final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
  22. bitmap,
  23. (int) (bitmap.getWidth() * 0.1),
  24. (int) (bitmap.getHeight() * 0.1),
  25. true
  26. );
  27. runOnUiThread(new Runnable() {
  28. @Override
  29. public void run() {
  30. imageView.setImageBitmap(scaledBitmap);
  31. }
  32. });
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }).start();
  38. }
  39. }

注意:我已经去掉了HTML编码中的",以便代码在Java中正确显示。

英文:

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:

  1. public class MainActivity extends AppCompatActivity {
  2. private static final String imageUrl
  3. = "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. final ImageView imageView = findViewById(R.id.imageView);
  9. new Thread(new Runnable() {
  10. @Override
  11. public void run() {
  12. URL url = null;
  13. try {
  14. url = new URL(imageUrl);
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. }
  18. try (BufferedInputStream bufferedInputStream
  19. = new BufferedInputStream(url.openStream())) {
  20. Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
  21. final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
  22. bitmap,
  23. (int) (bitmap.getWidth() * 0.1),
  24. (int) (bitmap.getHeight() * 0.1),
  25. true
  26. );
  27. runOnUiThread(new Runnable() {
  28. @Override
  29. public void run() {
  30. imageView.setImageBitmap(scaledBitmap);
  31. }
  32. });
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }).start();
  38. }
  39. }

huangapple
  • 本文由 发表于 2020年4月10日 12:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定