【问题标题】:Displaying Images in Android throws Out of Memory Error在 Android 中显示图像会引发内存不足错误
【发布时间】:2012-07-05 12:55:10
【问题描述】:

我在ViewPager.中显示图片,显示的图片占用了很多空间。显示几张图片后,它会抛出java.lang.outofMemoryError: Bitmap size exceeds VM bugdet.

对可能出现的问题有什么建议吗?

public Object instantiateItem(View pager, int position) {

    LayoutInflater inflater = (LayoutInflater) pager.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.fotopager_buttons, null);

    TextView text = ((TextView) view.findViewById(R.id.textView1));
    text.setText(Global.totalpaging_titlearray[position]);

    TextView textprice = ((TextView) view.findViewById(R.id.textprice));
    textprice.setText("SAR " + Global.totalpaging_pricearray[position]);

    TextView textdesc = ((TextView) view.findViewById(R.id.textdescription));
    textdesc.setText(Global.totalpaging_descarray[position]);

    ImageView imageView1 = ((ImageView) view.findViewById(R.id.image));
    bimage = getBitmapFromURL(Global.totalpaging_array[position]);
    imageView1.setImageBitmap(bimage);
    ((ViewPager) pager).addView(view);

    return view;
}

【问题讨论】:

  • @M Mohsin Naeem:我用谷歌搜索了很多,然后才发布了这个问题。
  • @nunu:我不想调整位图的大小,但我想通过释放可绘制对象来管理内存。

标签: android out-of-memory


【解决方案1】:

据我了解,您正在查看寻呼机的页面上显示图像。您的视图寻呼机有多少页?

如果您在一页中的位图超出 VM 预算,请考虑调整位图大小或解码为低记忆颜色格式,例如 RGB565 或 ARGB444。

如果没有代码,就无法确定您是否正确地为视图寻呼机实例化页面。您可以发布 PagerAdapter 类 instantiateItem(View view, int position) 方法的代码吗?

【讨论】:

  • @Soniya 您没有显示getBitmapFromURL() 方法的代码。我猜你已经使用 Bitmap Factory 从 url 下载位图。无论如何here is a page我可以指向处理大型位图
【解决方案2】:

您在图像非常大之前就遇到了这个问题。它们是非常高分辨率的图像,因此具有非常大的尺寸。尝试使用任何软件(如 Photoshop)减小这些图像的大小或分辨率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多