gpt4 book ai didi

Android ViewPager OutOfMemory异常

转载 作者:行者123 更新时间:2023-11-29 20:11:57 26 4
gpt4 key购买 nike

我实现了一个 ViewPager,但我在 instantiateItem 方法中遇到了 OutOfMemory 异常:

@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = QPhotoGallery.this;
mImageViewForPager = new ImageView(context);
mImageViewForPager.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

mImageForView = mImages.get(position);

File imgFile = new File(mImageForView.getPath());
if (imgFile.exists()) {
mImageViewForPager.setImageURI(Uri.fromFile(imgFile));
if (mImageForView.getOrientation() == ExifInterface.ORIENTATION_ROTATE_90)
mImageViewForPager.setRotation(90);
if (mImageForView.getOrientation() == ExifInterface.ORIENTATION_ROTATE_180)
mImageViewForPager.setRotation(180);
if (mImageForView.getOrientation() == ExifInterface.ORIENTATION_ROTATE_270)
mImageViewForPager.setRotation(270);
}

...
}

这一行:

mImageViewForPager.setImageURI(Uri.fromFile(imgFile));

导致异常。有人有想法吗?

最佳答案

内存不足异常

当使用可用平台资源无法满足内存请求时抛出。此类请求可能由正在运行的应用程序或 VM 的内部功能发出。

You are dealing with large bitmaps and loading all of them at run time. You have to deal very carefully with large bitmaps by loading the size that you need not the whole bitmap at once and then do scaling.

问题来了

mImageViewForPager.setImageURI(Uri.fromFile(imgFile));

imgFile 太大(Big Size)。这就是为什么有问题。缩小其尺寸(决议)。您可以在 list 中添加 android:largeHeap="true" 。希望这会有所帮助。

https://developer.android.com/intl/es/training/displaying-bitmaps/index.html

关于Android ViewPager OutOfMemory异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34630993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com