带有 ImageView 的 ViewPager 给出“java.lang.OutOfMemoryError:位图大小超出 VM 预算";

ViewPager with ImageView gives quot;java.lang.OutOfMemoryError: bitmap size exceeds VM budgetquot;(带有 ImageView 的 ViewPager 给出“java.lang.OutOfMemoryError:位图大小超出 VM 预算;)

本文介绍了带有 ImageView 的 ViewPager 给出“java.lang.OutOfMemoryError:位图大小超出 VM 预算";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 ViewPager 来显示图像.当我推进一些页面时,我得到一个 java.lang.OutOfMemoryError: bitmap size exceeded VM budget 错误.

I made a ViewPager to display images. When I advance some pages I get an java.lang.OutOfMemoryError: bitmap size exceeds VM budget error.

关于这个问题还有更多问题,但我没有找到解决方案(BitMap.RecycleSystem.gc() 等).如果您有任何建议或解决方案,请告诉我!

There are more questions about this issue but I did not find the solution (BitMap.Recycle, System.gc() etc). If you have a suggestion or solution please let me know!

PNG 为 628KB、478KB、587KB、132KB、139KB、149KB、585KB(崩溃).

The PNG's are 628KB, 478KB, 587KB, 132KB, 139KB, 149KB, 585KB (crash).

如果您有其他解决方案(滚动图像,如页面),请告诉我!

If you have an other solution (scroll images like pages) for me let met know!

我的代码:

package nl.ipear.vngmagazine;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class ShowMagazine2 extends FragmentActivity {
    private ViewPager myPager;
    private MyPagerAdapter myPagerAdapter;

    private static int NUM_PAGES = 15;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmagazine);

        myPager = (ViewPager)findViewById(R.id.viewpager1);
        myPagerAdapter = new MyPagerAdapter();
        myPager.setAdapter(myPagerAdapter);

        return;
    }

    private class MyPagerAdapter extends PagerAdapter{
        @Override
        public int getCount() {
            return NUM_PAGES;
        }

        @Override
        public Object instantiateItem(View collection, int position) {      
            String location = Environment.getExternalStorageDirectory() + "/MYData/" + "2012-02/";

            // Inflate and create the view
            LayoutInflater layoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.magazinepageview, null);

            ImageView imageView = (ImageView) view.findViewById(R.id.magazinePageImage);
            String fileName = String.format("%s%s%02d%s", location, "2012-02_Page_", position + 1, ".png");
            Log.v("PNG", fileName);
            imageView.setImageBitmap(BitmapFactory.decodeFile(fileName));

            ((ViewPager) collection).addView(view,0);

            return view;
        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);           
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
        }

        @Override
        public void finishUpdate(View arg0) {}

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {}   
    }
}

推荐答案

在这里找到答案:Android:java.lang.OutOfMemoryError: 无法分配 23970828 字节分配,2097152 字节和 2MB 直到 OOM

只需在 AndroidManifest 内的 Application 标签中添加 android:hardwareAccelerated="false"android:largeHeap="true" 即可:

Just add android:hardwareAccelerated="false" and android:largeHeap="true" in your Application tag inside AndroidManifest:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="false"
        android:largeHeap="true">

这篇关于带有 ImageView 的 ViewPager 给出“java.lang.OutOfMemoryError:位图大小超出 VM 预算";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 ImageView 的 ViewPager 给出“java.lang.OutOfMemoryError:位图大小超出 VM 预算";

基础教程推荐