3

我正在开发一个演示应用程序,其中我正在播放这么多图像(位图)。我正在使用全局位图对象在多个活动中显示相同的图像。但是,当我尝试使用 Bitmap.createBitmap(...) 创建位图时,使用它时会出现 OutOfMemoryError。我尝试使用此代码,但仍然遇到相同的错误,它使我的应用程序崩溃并通过 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)
4

5 回答 5

6

Ah - the many wonderful battles one may have with Google's implementation of bitmap handling in Android.

I would tend to do some memory profiling anytime I hit the roof with memory - very often (although not always) it is because one is doing something wrong (i.e., unexpectedly memory intensive). Just browsing the code, I am a little bit puzzled by the need to force the code through three streams, just to end up with a bitmap again. I feel as if I'm missing some information for me to make sense of this, but I'd definitely take a look at just how much memory you're allocating in each of those steps if I were you (heap dump -> hprof-conv -> memory analyzer is your friend).

However, I suspect that what you may be looking for is BitmapRegionDecoder. I actually only recently discovered this myself, but I think it does pretty much exactly what you are after - allows you to decode a region from a bitmap (inputstream) without loading the entire bitmap into memory (you can also get the width/height). Only downside is that it is Android 2.3.3+ only, but these days that >90% of the market so that should not be a huge problem. I've done some pretty neat work with this (smooth pan and zoom of 6000x4000 image) - it is definitely memory effective if used right.

[EDIT]

I recently open-source my own dumb implementation using BitmapRegionDecoder which handles pan, fling, and pinch-zoom of a very large bitmap. You can find the code (ASL2.0) at https://github.com/micabyte/android_game

The class you want to look at is BitmapSurfaceRenderer. So far I've tested it with up to 8000x4000 images, and it seems to work very well. When I get the time, I'll put up a simple example app that demonstrates how it is used as well.

于 2013-02-26T20:10:18.763 回答
4

Try to get rid of outstream before you decode, e.g nulling the pointer (closing this specific stream will do nothing due to the type of the stream).

You're doing quite an extensive operation here so we need to be as light as possible...

Your code also lacks closing the input stream (which is not the bug here, but still should be done after you decodeStream), using close().

Try and look into Nostras ImageLoader, as it efficiently handles loading images into specific sized containers, i.e resizing and compressing them, before you will need to handle them. It also supports content uris which will help you all at once.

于 2013-02-28T03:42:48.410 回答
2

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

于 2013-02-21T10:48:14.240 回答
1

Bitmap.recycle()在使 Bitmap 对象无效之前使用该方法。

以下是示例:

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

接下来,您可以myBitmapImage在不调用的情况下进行更改System.gc(),如下所示:

 setMyBitmap(anotherBitmap);
   setMyBitmap(null);
于 2013-02-28T08:01:52.073 回答
1

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

于 2013-02-25T07:03:50.927 回答