我已经实现了gridview和全屏图像视图。一切正常,直到我喜欢这个。 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);
在Stackoverflow中检查了解决方案,我得到了这个解决方案,但得到了同样的错误。
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.同样的问题和我的全屏图像上的问题看起来很模糊。
答案 0 :(得分:0)
你不能将整个大小的位图放在内存中 - 它很重。你应该先缩小它。最简单的方法是不要重新发明自行车,而是选择Android universal image loader或Aquery库。它们都包含如何进行图像网格视图的示例。
答案 1 :(得分:0)
感谢各位帮助我找到解决方案。
而不是使用位图来解码图像视图中显示的图像。我们可以使用毕加索来加载图片。
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;
}
这解决了我的内存不足错误。如果还有更好的做法,请告诉我。