Android中Bitmap的OutOfMemoryError

时间:2013-02-21 10:13:22

标签: android bitmap

我正在开发一个演示应用程序,其中我正在播放如此多的图像(位图)。我使用Global Bitmap对象将同一图像显示到多个Activity中。但是当我尝试使用Bitmap.createBitmap(...)创建Bitmap时,我正在使用OutOfMemoryError。我尝试使用this code,但我仍然遇到同样的错误,它正在崩溃我的应用程序并通过OutOfMemoryError。

我被困在这上面了。任何人都可以有任何解决方案来避免这种情况。??

提前致谢。

裁剪图像后我得到位图,以便我使用下面的代码来解码位图大小。

public static Bitmap loadFromBitmap( Context context, Bitmap b, int maxW, int maxH ) throws IOException {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = null;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inJustDecodeBounds = true;
        BufferedInputStream stream = null;
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        b.compress(CompressFormat.JPEG, 100, outstream);
        InputStream is = new java.io.ByteArrayInputStream(outstream.toByteArray());

        stream = new BufferedInputStream(is, 16384 );
        if ( stream != null ) {
            options.inSampleSize = calculateInSampleSize( options, maxW, maxH );
            stream = null;
            stream = new BufferedInputStream(is, 16384 );
        } else {
            return null;
        }
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        bitmap = BitmapFactory.decodeStream( stream, null, options );
        if ( bitmap != null ) bitmap = BitmapUtils.resizeBitmap( bitmap, maxW, maxH );
        return bitmap;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

错误:

02-21 15:32:31.160: E/AndroidRuntime(2951): FATAL EXCEPTION: main
02-21 15:32:31.160: E/AndroidRuntime(2951): java.lang.OutOfMemoryError
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.Activity.performCreate(Activity.java:4465)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.os.Looper.loop(Looper.java:137)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at android.app.ActivityThread.main(ActivityThread.java:4447)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 15:32:31.160: E/AndroidRuntime(2951):     at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:6)

啊 - 谷歌在Android中实现位图处理的许多精彩战斗。

我倾向于随时用内存进行一些内存分析 - 很多时候(虽然并非总是如此)这是因为一个人做错了什么(即意外的内存密集)。只是浏览代码,我有点困惑的是需要强制代码通过三个流,只是为了再次结束位图。我觉得好像我错过了一些让我理解这一点的信息,但是如果我是你的话,我肯定会看看你在每个步骤中分配了多少内存(堆转储 - &gt; hprof-conv - &gt;内存分析器是你的朋友。)

但是,我怀疑你可能正在寻找的是BitmapRegionDecoder。我实际上最近才发现这一点,但我认为它确实完全符合您的要求 - 允许您从位图(输入流)解码区域而无需将整个位图加载到内存中(您也可以获得宽度/高度) 。唯一的缺点是它只是Android 2.3.3+,但是这些天大约90%的市场不应该是一个巨大的问题。我已经完成了一些相当简洁的工作(平滑的平移和6000x4000图像的缩放) - 如果正确使用它肯定是内存有效。

[编辑]

我最近使用BitmapRegionDecoder开源我自己的哑实现,它可以处理非常大的位图的平移,拖动和缩放。您可以在https://github.com/micabyte/android_game

找到代码(ASL2.0)

您要查看的类是BitmapSurfaceRenderer。到目前为止,我已经用高达8000x4000的图像测试了它,它似乎运行得很好。当我有空的时候,我会提出一个简单的示例应用程序,演示如何使用它。

答案 1 :(得分:4)

尝试在解码前删除outstream,例如使指针为空(由于流的类型,关闭此特定流将不执行任何操作)。

你在这里进行了大量的操作,所以我们需要尽可能轻松......

您的代码也缺少关闭输入流(这不是错误,但仍应在decodeStream之后完成),使用close()。

尝试并研究Nostras ImageLoader,因为它有效地处理将图像加载到特定大小的容器中,即在需要处理它们之前调整大小和压缩它们。它还支持内容uris,它将立即帮助您。

答案 2 :(得分:2)

使用options.inPurgeable = true和op.inInputShareable = true编辑代码 并在代码中执行一些内存保存方法。

答案 3 :(得分:1)

尝试在每次调用System.gc()之前添加loadFromBitmap()

答案 4 :(得分:1)

在nullfying Bitmap对象之前使用方法Bitmap.recycle()

以下是样本:

 public final void setBitmap(Bitmap bitmap) {
   if (this.myBitmapImage != null) {
      this.myBitmapImage.recycle();
   }
    this.myBitmapImage = bitmap;
 }

接下来,您可以更改myBitmapImage而无需调用System.gc(),如下所示:

 setMyBitmap(anotherBitmap);
   setMyBitmap(null);