本文介绍了内存不足错误ImageView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid的编程,我得到一个错误,说我的应用程序运行的内存,这exampled我从一本书上抄,它正在与小图片的分辨率,但是当我加了几张照片带更大的分辨率超出出现内存不足的错误,可能是我做错事,或只是不知道所有我应该还没有处理图像的工作,如果有人知道我应该怎么改变,因此,这个错误不会再次出现,恳求帮助。谢谢期待!

该人士$ ​​C $ C:

 公共类ImageViewsActivity延伸活动{
//要显示的图像
整数[] imageIDs = {
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5
};
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    最后ImageView的IV =(ImageView的)findViewById(R.id.image1);

    画廊画廊=(图库论坛)findViewById(R.id.gallery);
    gallery.setAdapter(新ImageAdapter(本));
  gallery.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
            Toast.makeText(getBaseContext(),PIC+(位置+ 1)+选择,Toast.LENGTH_SHORT).show();

            //显示选择的图像
            尝试{iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
              iv.setImageResource(imageIDs [位置]);}赶上(OutOfMemoryError异常E){
                     iv.setImageBitmap(空);
                }
        }
    });


}

公共类ImageAdapter扩展了BaseAdapter {
    私人上下文的背景下;
    私人诠释itemBackground;

    公共ImageAdapter(上下文C){
        上下文= C;
        //设置样式
        TypedArray A = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
        a.recycle();
    }

    //返回图像的数量
    公众诠释getCount将(){
        // TODO自动生成方法存根
        返回imageIDs.length;
    }

    //返回一个项目的ID
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }

    //返回一个项目的ID
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }

    //返回一个ImageView的观点
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        // TODO自动生成方法存根
        ImageView的IV =新ImageView的(上下文);
        iv.setImageResource(imageIDs [位置]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(新Gallery.LayoutParams(150120));
        iv.setBackgroundResource(itemBackground);

        返回IV;
    }
}}
 

错误的位置:

 十月4日至18日:38:31.661:D / dalvikvm(10152):调试器分离;对象注册了442项
10月4号至18号:38:31.661:D / AndroidRuntime(10152):关闭虚拟机
10月4号至18号:38:31.661:W / dalvikvm(10152):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d820)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):致命异常:主要
10月4号至18号:38:31.691:E / AndroidRuntime(10152):java.lang.OutOfMemoryError:位图大小超过VM预算
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.Bitmap.nativeCreate(本机方法)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.Bitmap.createBitmap(Bitmap.java:499)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.Bitmap.createBitmap(Bitmap.java:466)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:371)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.BitmapFactory.finishDe code(BitmapFactory.java:539)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:508)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:365)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:728)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.content.res.Resources.loadDrawable(Resources.java:1740)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.content.res.Resources.getDrawable(Resources.java:612)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.ImageView.resolveUri(ImageView.java:520)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.ImageView.setImageResource(ImageView.java:305)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在image.view.GalleryView $ ImageAdapter.getView(GalleryView.java:95)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.Gallery.makeAndAddView(Gallery.java:776)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.Gallery.fillToGalleryLeft(Gallery.java:695)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.Gallery.trackMotionScroll(Gallery.java:406)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.widget.Gallery $ FlingRunnable.run(Gallery.java:1397)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.os.Handler.handleCallback(Handler.java:618)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.os.Handler.dispatchMessage(Handler.java:123)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.os.Looper.loop(Looper.java:154)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在android.app.ActivityThread.main(ActivityThread.java:4668)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在java.lang.reflect.Method.invokeNative(本机方法)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在java.lang.reflect.Method.invoke(Method.java:552)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:917)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
10月4号至18号:38:31.691:E / AndroidRuntime(10152):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

使用

 ((BitmapDrawable)imageView.getDrawable())getBitmap()循环()。
 

更改为新的图像前!!

I'm new in Android programming and I got an error that says that my app run out of memory, this exampled I copied from a book and it is working with small pictures resolution, but when I added a few pictures with a bigger resolution out of memory error appears, may be I do something wrong or just don't know all I should yet to work with images, if anyone know what should i change so that this error won't appear again, pleas help. Thank you anticipate!

The source code:

public class ImageViewsActivity extends Activity {
//the images to display
Integer[] imageIDs={
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ImageView iv=(ImageView) findViewById(R.id.image1);

    Gallery gallery=(Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));
  gallery.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View v, int position, long id){
            Toast.makeText(getBaseContext(), "pic"+(position+1)+" selected", Toast.LENGTH_SHORT).show();

            //display the image selected
            try{iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
              iv.setImageResource(imageIDs[position]);}catch(OutOfMemoryError e){
                     iv.setImageBitmap(null);
                }
        }
    });


}

public class ImageAdapter extends BaseAdapter{
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c){
        context=c;
        //setting the style
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    //returns the number of images
    public int getCount() {
        // TODO Auto-generated method stub
        return imageIDs.length;
    }

    //returns the ID of an item
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    //returns the ID of an item
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    //returns an ImageView view
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView iv= new ImageView(context);
        iv.setImageResource(imageIDs[position]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));
        iv.setBackgroundResource(itemBackground);

        return iv;
    }
}}

ERROR HERE:

04-18 10:38:31.661: D/dalvikvm(10152): Debugger has detached; object registry had 442 entries
04-18 10:38:31.661: D/AndroidRuntime(10152): Shutting down VM
04-18 10:38:31.661: W/dalvikvm(10152): threadid=1: thread exiting with uncaught exception (group=0x4001d820)
04-18 10:38:31.691: E/AndroidRuntime(10152): FATAL EXCEPTION: main
04-18 10:38:31.691: E/AndroidRuntime(10152): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.Bitmap.nativeCreate(Native Method)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.Bitmap.createBitmap(Bitmap.java:499)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.Bitmap.createBitmap(Bitmap.java:466)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:371)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:539)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:508)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:365)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:728)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.content.res.Resources.loadDrawable(Resources.java:1740)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.content.res.Resources.getDrawable(Resources.java:612)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.ImageView.resolveUri(ImageView.java:520)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.ImageView.setImageResource(ImageView.java:305)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at image.view.GalleryView$ImageAdapter.getView(GalleryView.java:95)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.Gallery.makeAndAddView(Gallery.java:776)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.Gallery.fillToGalleryLeft(Gallery.java:695)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.Gallery.trackMotionScroll(Gallery.java:406)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.widget.Gallery$FlingRunnable.run(Gallery.java:1397)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.os.Handler.handleCallback(Handler.java:618)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.os.Handler.dispatchMessage(Handler.java:123)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.os.Looper.loop(Looper.java:154)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at android.app.ActivityThread.main(ActivityThread.java:4668)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at java.lang.reflect.Method.invoke(Method.java:552)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
04-18 10:38:31.691: E/AndroidRuntime(10152):    at dalvik.system.NativeStart.main(Native Method)
解决方案

Use

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

Before change to new image!!

这篇关于内存不足错误ImageView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:30