ViewPager java.lang.OutOfMemoryError:位图大小超出VM预算
可能重复:ViewPager java.lang.OutOfMemoryError:位图大小超出VM预算,java,android,android-viewpager,Java,Android,Android Viewpager,可能重复: 我使用ViewPager显示资源文件夹中的一组图像,如果我的图像很小,一切正常 但当我用高清图像替换它时,我需要它出现在我的应用程序中,它给了我以下错误: java.lang.OutOfMemoryError:位图大小超出VM预算 注1: 我现在有5个图像在我的代码中进行测试,但最后我将有大约30个高清晰度图像 注2: 我想知道为什么会发生这种情况,我是android新手,也是第一次使用viewpager类,在我在另一个应用程序中使用gallery类,有30多张高清图像,也没有例外
我使用ViewPager显示资源文件夹中的一组图像,如果我的图像很小,一切正常 但当我用高清图像替换它时,我需要它出现在我的应用程序中,它给了我以下错误: java.lang.OutOfMemoryError:位图大小超出VM预算 注1: 我现在有5个图像在我的代码中进行测试,但最后我将有大约30个高清晰度图像 注2: 我想知道为什么会发生这种情况,我是android新手,也是第一次使用viewpager类,在我在另一个应用程序中使用gallery类,有30多张高清图像,也没有例外 任何建议都将不胜感激,非常感谢 我的代码: logcat堆栈:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:462)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:488)
at com.test.demo.MyPagerAdapter.<init>(MyPagerAdapter.java:42)
at com.test.demo.MainActivity.onCreate(MainActivity.java:15)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
MyPagerAdapter
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<ImageView> mViewsList;
private Context mContext = null;
public MyPagerAdapter(Context context) {
mContext = context;
mViewsList = new ArrayList<ImageView>();
Resources resource = mContext.getResources();
Bitmap bMap1 = BitmapFactory.decodeResource(resource,
R.drawable.one);
ImageView image1 = new ImageView(mContext);
image1.setImageBitmap(bMap1);
mViewsList.add(image1);
Bitmap bMap2 = BitmapFactory.decodeResource(resource,
R.drawable.two );
ImageView image2 = new ImageView(mContext);
image2.setImageBitmap(bMap2);
mViewsList.add(image2);
Bitmap bMap3 = BitmapFactory.decodeResource(resource,
R.drawable.three);
ImageView image3 = new ImageView(mContext);
image3.setImageBitmap(bMap3);
mViewsList.add(image3);
Bitmap bMap4 = BitmapFactory.decodeResource(resource,
R.drawable.four);
ImageView image4 = new ImageView(mContext);
image4.setImageBitmap(bMap4);
mViewsList.add(image4);
Bitmap bMap5 = BitmapFactory.decodeResource(resource,
R.drawable.five);
ImageView image5 = new ImageView(mContext);
image5.setImageBitmap(bMap5);
mViewsList.add(image5);
}
@Override
public int getCount() {
return mViewsList.size();
}
@Override
public Object instantiateItem(View view, int position) {
View myView = mViewsList.get(position);
((ViewPager) view).addView(myView);
return myView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(View view, int arg1, Object object) {
((ViewPager) view).removeView((ImageView) object);
}
}
公共类MyPagerAdapter扩展了PagerAdapter{
私有ArrayList mViewsList;
私有上下文mContext=null;
公共MyPagerAdapter(上下文){
mContext=上下文;
mViewsList=newarraylist();
Resources resource=mContext.getResources();
位图bMap1=BitmapFactory.decodeResource(资源,
R.可抽出式(1);
ImageView image1=新的ImageView(mContext);
image1.setImageBitmap(bMap1);
mViewsList.add(图1);
位图bMap2=BitmapFactory.decodeResource(资源,
R.drawable.2);
ImageView image2=新的ImageView(mContext);
image2.setImageBitmap(bMap2);
mViewsList.add(图2);
位图bMap3=BitmapFactory.decodeResource(资源,
R.可抽出式(3);
ImageView image3=新的ImageView(mContext);
image3.setImageBitmap(bMap3);
mViewsList.add(图3);
位图bMap4=BitmapFactory.decodeResource(资源,
R.可抽出式(4);
ImageView image4=新的ImageView(mContext);
image4.setImageBitmap(bMap4);
mViewsList.add(图4);
位图bMap5=BitmapFactory.decodeResource(资源,
R.可抽出式(5);
ImageView image5=新的ImageView(mContext);
image5.setImageBitmap(bMap5);
mViewsList.add(图5);
}
@凌驾
public int getCount(){
返回mViewsList.size();
}
@凌驾
公共对象实例化项(视图,int位置){
View myView=mViewsList.get(位置);
((ViewPager)视图).addView(myView);
返回myView;
}
@凌驾
公共布尔值isViewFromObject(视图,对象对象){
返回视图==对象;
}
@凌驾
公共项(视图,int arg1,Object对象){
((ViewPager)视图)。移除视图((ImageView)对象);
}
}
位图的大小非常大,因此请在加载之前尝试缩放位图。
缩放位图的语法
Bitmap bitmapName= Bitmap.createScaledBitmap(sourceBitmap, width, height,
true);
其中,宽度和高度是您希望图像的大小。您的问题是,在真正需要之前,您正在创建一组位图
在
实例化视图
方法中,创建图像视图
,加载位图并将其设置在那里。如果需要,请使用ArrayList保存资源int,以了解需要加载哪些图像。通过这种方式,垃圾收集将根据需要删除位图,并且不会导致内存问题。您的适配器不应该按照您现有的方式编写。您应该只在InstanceItem方法中解码位图
private Context context;
private ArrayList<Integer> mResourceList;
private Resources resource;
public MyPagerAdapter(Context context) {
this.context = context;
resource = context.getResources();
mResourceList = new ArrayList<Integer>();
mResourceList.add(R.drawable.one);
mResourceList.add(R.drawable.two);
mResourceList.add(R.drawable.three);
mResourceList.add(R.drawable.four);
mResourceList.add(R.drawable.five);
}
@Override
public Object instantiateItem(View view, int position) {
ImageView myView = new ImageView(context);
Bitmap bitmap = BitmapFactory.decodeResource(resource, mResourceList.get(position) );
myView.setImageBitmap(bitmap);
((ViewPager) view).addView(myView);
return myView;
}
希望这有帮助 您是否看到StackOverflow右侧题为“相关”的问题旁边的这一列。你看到这堆复制品了吗?如果是这样的话,就躲在那里。也请掌握你的搜索技能…。@WebnetMobile.com在我发布问题之前我看到了这篇文章,但我是android新手,也是第一次使用viewpager类,请你解释一下为什么会发生这种情况,它与viewpager类或位图有关,因为我在gallery课程之前在另一个应用程序中使用了30多个高清大尺寸图像,但什么都没有发生,谢谢我在上面的评论中提到,我是android新手,第一次同时使用viewpager和bitmap,所以请你用代码提供答案,谢谢我用代码显示图像,然后我滚动第一个图像和第二个图像,直到现在还可以,但当我滚动第三个图像时,它会再次强制关闭,但有一个例外:java.lang.OutOfMemoryError:位图大小超过VM预算任何建议,感谢4000*2000大小约为4.5mb的每个图像可能我以错误的方式实现了您的代码,我没有经验,请帮我做另一件事,我以前在画廊课上用过更大的图片,没问题,为什么我亲爱的你的图片太大了。试着把它们降到2000 x 500左右,看看这是否解决了你的问题。请在我尝试缩小图像比例后再看我的新帖子谢谢,
private Context context;
private ArrayList<Integer> mResourceList;
private Resources resource;
public MyPagerAdapter(Context context) {
this.context = context;
resource = context.getResources();
mResourceList = new ArrayList<Integer>();
mResourceList.add(R.drawable.one);
mResourceList.add(R.drawable.two);
mResourceList.add(R.drawable.three);
mResourceList.add(R.drawable.four);
mResourceList.add(R.drawable.five);
}
@Override
public Object instantiateItem(View view, int position) {
ImageView myView = new ImageView(context);
Bitmap bitmap = BitmapFactory.decodeResource(resource, mResourceList.get(position) );
myView.setImageBitmap(bitmap);
((ViewPager) view).addView(myView);
return myView;
}
BitmapFactory.Options options = new BitmapFactory.Options()
options.inSampleSize = 2
Bitmap bitmap = BitmapFactory.decodeResource(resource, mResouceList.get(position), options );