在android上创建图库时出现内存不足错误

问题描述:

在eclipse中创建android图库时出现此错误。当我只添加两个图像时,它运行良好,但是当我向图库添加更多图像时,会出现此错误。任何帮助,将不胜感激!在android上创建图库时出现内存不足错误

package com.hospital; 
import com.hospital.R; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 


import android.view.View; 
import android.view.ViewGroup; 

import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.Toast; 

import android.widget.AdapterView.OnItemClickListener; 


public class myGallery extends Activity { 

    private final Integer[] pic = { 
      R.drawable.sample_1, 
      R.drawable.sample_2, 
      R.drawable.sample_3 


    }; 
    ImageView imageview; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gallery); 

     Gallery gallery = (Gallery)findViewById(R.id.pics); 
     gallery.setAdapter(new ImageAdapter(this)); 

     imageview = (ImageView)findViewById(R.id.ImageView01); 
     gallery.setOnItemClickListener(new OnItemClickListener() 
     { 
      public void onItemClick(AdapterView <?> arg0, View arg1, int arg2, long arg3) { 
       Toast.makeText(myGallery.this, "" + arg0, Toast.LENGTH_SHORT).show(); 
       imageview.setImageResource(pic[arg2]); 
      } 
     }); 
     registerForContextMenu(gallery); 
    } 

public class ImageAdapter extends BaseAdapter { 
    private final int mGalleryItemBackground; 
    private final Context mContext; 
    private final Integer[] pic = { 
      R.drawable.sample_1, 
      R.drawable.sample_2, 
      R.drawable.sample_3 


    }; 

     public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray attr = c.obtainStyledAttributes(R.styleable.photoGallery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.photoGallery_android_galleryItemBackground, 1); 
     attr.recycle(); 
    } 

    public int getCount() { 
     return pic.length; 
    } 
    public Object getItem(int arg0) { 
     return arg0; 
    } 

    public long getItemId(int arg0) { 
     return arg0; 
    } 

    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     ImageView imageView = new ImageView(mContext); 

     imageView.setImageResource(pic[arg0]); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 
} 
} 
+0

检查这个问题和答案。 http://*.com/questions/10161079/out-of-memory-exception-in-android-when-adding-some-image-buttons/10161107#10161107 – erbsman 2012-04-17 10:35:02

+0

你能给你的错误日志吗? – 2012-04-17 10:35:34

内存不足是使用大量位图的未来时,通常误差只,低脂减少误差的方法是调用System.gc();System.gc()在您的代码中,Java VM可能会或可能不会在运行时决定在该点执行垃圾回收。在完成与他们的业务后,请尝试在您的位图上拨打"Bitmap".recycle()。这将帮助您最大限度地减少此错误。

+0

此解决方案不起作用。 访问以下提到的线程以获取正确的图片为什么会发生此异常。 http://*.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – 2012-04-17 11:42:58

谷歌已经提供了解决您面临的问题的方法,您可以通过以下链接访问它。

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

希望这有助于。