在 android 中显示全屏图像时出现内存不足错误

Getting the Out of memory error when displaying full screen image in android

我实现了网格视图和全屏图像视图。一切正常,直到我喜欢这个。 1. 我将打开我的网格视图并单击任何缩略图图像并全屏显示该图像。然后我将从图像视图返回到网格视图并单击其他缩略图。如果我重复这些步骤 6 次。当我第 7 次单击缩略图时,我会遇到内存不足的错误。

我在图像视图中使用视图寻呼机来滚动图像。

public Object instantiateItem(ViewGroup container, int position){
        TouchImageView imageView;

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

        imageView = (TouchImageView) view.findViewById(R.id.imgDisplay);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        bitmap = BitmapFactory.decodeFile(imagePaths.get(position), options);

在 Whosebug 中查看了解决方案,我得到了这个解决方案,但出现了同样的错误。

        if(bitmap!= null && bitmap.isRecycled()){
                bitmap.recycle();
                bitmap = null;
            }

下面是解决方案。

        imageView.setImageBitmap(bitmap);
        container.addView(view);
        return view;
    }

我的 logcat 详细信息。

02-20 19:39:40.265  31543-31543/com.android.exampleI/art﹕ Clamp target GC heap from 100MB to 96MB
02-20 19:39:40.265  31543-31543/com.android.example I/art﹕ GcCauseForAlloc partial concurrent mark sweep GC freed 178(8KB) AllocSpace objects, 1(16KB) LOS objects, 0% free, 92MB/93MB, paused 1.872ms total 20.641ms
02-20 19:39:40.305  31543-31543/com.android.example I/art﹕ Clamp target GC heap from 100MB to 96MB
02-20 19:39:40.305  31543-31543/com.android.example I/art﹕ GcCauseForAlloc concurrent mark sweep GC freed 8(8KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 92MB/93MB, paused 9.165ms total 39.789ms
02-20 19:39:40.305  31543-31543/com.android.example I/art﹕ Forcing collection of SoftReferences for 3MB allocation
02-20 19:39:40.337  31543-31543/com.android.example I/art﹕ Clamp target GC heap from 100MB to 96MB
02-20 19:39:40.338  31543-31543/com.android.example I/art﹕ GcCauseForAlloc concurrent mark sweep GC freed 7(224B) AllocSpace objects, 0(0B) LOS objects, 0% free, 92MB/93MB, paused 2.706ms total 32.142ms
02-20 19:39:40.338  31543-31543/com.android.example E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 3686412 byte allocation with 498496 free bytes"
02-20 19:39:40.355  31543-31543/com.android.example D/skia﹕ --- decoder->decode returned false
02-20 19:39:40.375  31543-31543/com.android.example D/AndroidRuntime﹕ Shutting down VM
02-20 19:39:40.408  31543-31543/com.android.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.android.example, PID: 31543
    java.lang.OutOfMemoryError: Failed to allocate a 3686412 byte allocation with 498496 free bytes
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
            at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:376)
            at com.android.example.FullScreenImageAdapter.instantiateItem(FullScreenImageAdapter.java:53)
            at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1021)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:16540)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
            at android.view.View.measure(View.java:16540)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

我已经尝试过 bitmap.samppleSize = 4 和 8。我的全屏图像再次出现同样的问题,而且更多问题看起来很模糊。

您不能将整个大小的位图放入内存中 - 它太重了。你应该先缩小它。最简单的方法是不要重新发明自行车,而是使用 Android universal image loader or Aquery 图书馆。它们都包含如何对图像进行 gridview 的示例。

谢谢你们帮助我找到解决方案。

而不是使用位图解码图像视图中的图像显示。我们可以使用 picasso 加载图像。

public Object instantiateItem(ViewGroup container, int position){
        TouchImageView imageView;

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_fullscreen_image, container, false);
        Uri uri = Uri.fromFile(new File(String.valueOf(imagePaths.get(position))));
        imageView = (TouchImageView) view.findViewById(R.id.imgDisplay);

        Picasso.with(container.getContext())
                .load(uri)
               .into(imageView);

      container.addView(view);
        return view;
    } 

这解决了我的内存不足错误。如果有更好的办法请告诉我。