【问题标题】:android how to download an 1mb image file and set to ImageViewandroid如何下载1mb图像文件并设置为ImageView
【发布时间】:2011-09-13 18:10:02
【问题描述】:

我正在尝试下载一个 1mb 的图像文件,然后保存到位图以填充图像视图。 但显示错误

06-13 13:39:48.149: ERROR/AndroidRuntime(1782):
java.lang.OutOfMemoryError: bitmap size exceeds VM budget

如何下​​载大图?

【问题讨论】:

标签: android image memory-management


【解决方案1】:

我也遇到过同样的问题。在将下载的位图设置为您的ImageView 之前,您必须根据您的ImageView 的宽度和高度压缩您的位图。

你必须像这样创建一个缩放位图,

 bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
imgView.setImageBitmap(bm1);

或者像这样压缩你的位图,

            OutputStream fOut = null;
                        File file = new File(strDirectoy,imgname);
                            fOut = new FileOutputStream(file);

                        bm1.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            fOut.flush();
                            fOut.close();

                MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

【讨论】:

  • bm1=Bitmap.createScaledBitmap(bm, 300, 300,true); imgView.setImageBitmap(bm1);有时它工作,有时它不工作。你能告诉我为什么吗?
  • 如果没有尝试我给出的下一个方法。这将压缩并存储您的图像。之后尝试使用 uri 为 imageview 设置图像。
猜你喜欢
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 2016-02-03
相关资源
最近更新 更多