1

OutOfMemoryException作为一个新程序员,我对使用BitmapFactory.decodeStream(is,option)方法时完全混淆了哪个returns Bitmap对象。由于每个应用程序都有其virtual machine预算,如果超出预算,它就会抛出OutOfMemoryException并且应用程序崩溃bitmap很重。所以任何人都可以帮我解决这个问题。我必须将图像设置为ImageView使用setImageBitmap(bitmap)方法。除非直到 m 无法Bitmap参考,否则我该如何设置ImageView

4

5 回答 5

1

试试这样,它会工作..

   public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

}

于 2013-04-29T11:32:39.107 回答
0

android 开发参考包含大量适用于新 Android 开发者的示例和指南。

在这里,您可以找到一份开发指南,该指南解释了在应用程序中加载位图的最佳实践。

此解决方案是将内存中的图像缩小到 imageView 的大小以避免 OutOfMemoryException。因为对于非常大的图片,如果您的屏幕只有 720p 宽,则不需要加载每个像素。

于 2013-04-29T11:24:40.557 回答
0

我有同样的问题,所以我使用了以下选项

BitmapFactory.Options options=new BitmapFactory.Options();
options.inPreferQualityOverSpeed=false;
options.inSampleSize=10;
options.inScaled=false;
options.inTargetDensity=100;

我希望这也对你有用

于 2013-04-29T11:22:45.957 回答
0

如果您在模拟器上测试应用程序,请尝试VM heap通过编辑 AVD 来增加大小。希望这会帮助你。

于 2013-04-29T11:34:31.677 回答
0

尝试解码您的位图,然后使用它。

try this

  Bitmap  bitmap =   Bitmap.createScaledBitmap(ImageResource,desired height , desired width ,false);
于 2013-04-29T11:52:13.213 回答