请教一个关于gridview插入图片的bitmap溢出问题~

lovingPuff 发布于 2014/05/31 19:31
阅读 402
收藏 1

插入3张2M左右的图片程序就会死掉,估计是OOM,求大神解答


package com.example.mygod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class myselfLetter extends Activity{
public MyAdapter myadapter;
public Button addpic;
public Button back;
public GridView gv;
public LayoutInflater inflater;
public List<Bitmap> list = new ArrayList<Bitmap>();
public List<Uri> uriList = new ArrayList<Uri>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.myselfletter);
addpic = (Button)findViewById(R.id.addpic);
back = (Button)findViewById(R.id.back);
gv = (GridView)findViewById(R.id.gridview);
//    myadapter = new MyAdapter(this);
// gv.setAdapter(new MyAdapter(this));
// gv.setAdapter(myadapter);
inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   gv.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?>arg0,View arg1,int arg2,long arg3){
    ImageView imageview = (ImageView)arg1;
   // imageview.setDrawingCacheEnabled(true);
   // Bitmap obmp = Bitmap.createBitmap(imageview.getDrawingCache());
   // imageview.setDrawingCacheEnabled(false);
    Intent intent = new Intent();
    intent.setClass(myselfLetter.this, imageview.class);
  // intent.putExtra("bitmap",obmp);
    intent.putExtra("bitmap",uriList.get(arg2));
    startActivity(intent);
    }
   });
}
public void addpic(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode != RESULT_OK) {
  return;
 }
 if (data != null) {
  Uri mImageCaptureUri = data.getData();
  uriList.add(mImageCaptureUri);
  // 返回的Uri不为空时,那么图片信息数据都会在Uri中获得。如果为空,那么我们就进行下面的方式获
//取
  if (mImageCaptureUri != null) {
   Bitmap image;
   try {
    // 这个方法是根据Uri获取Bitmap图片的静态方法
    image = MediaStore.Images.Media.getBitmap(
      this.getContentResolver(), mImageCaptureUri);
    if (image != null) {
//      commentphoto.setImageBitmap(image);
//     myadapter.list.add(image);
    list.add(image);
//     image.recycle();
    myadapter = new MyAdapter(this,list); 
//     myadapter = new MyAdapter(this,uriList); 
    gv.setAdapter(myadapter);
//     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  } 
/*   else {
   Bundle extras = data.getExtras();
   if (extras != null) {
    // 这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取
//Bitmap图片
    Bitmap image = extras.getParcelable("data");
//     i.setImageBitmap(image);
    list.add(image);
    myadapter = new MyAdapter(this,list); 
    image.recycle();
    gv.setAdapter(myadapter);
   }
  }*/
 }     
   }
public void back(View v){
myselfLetter.this.finish();
}
class MyAdapter extends BaseAdapter{ 
       //上下文对象 
       private Context context; 
       //图片数组 
       private List<Bitmap> list;
 //      private List<Uri> list;
       MyAdapter(Context context,List list){ 
           this.context = context; 
           this.list = list;
       } 
       public int getCount() { 
      //     return imgs.length; 
        return list.size();
       } 
 
       public Object getItem(int item) { 
           return item; 
       } 
 
       public long getItemId(int id) { 
           return id; 
       } 
        
       //创建View方法 
       public View getView(int position, View convertView, ViewGroup parent) { 
            ImageView imageView; 
               if (convertView == null) { 
                   imageView = new ImageView(context); 
                   imageView.setLayoutParams(new GridView.LayoutParams(80, 80));//设置ImageView对象布局 
                   imageView.setAdjustViewBounds(false);//设置边界对齐 
                   imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//设置刻度的类型 
                   imageView.setPadding(0, 4, 0, 4);//设置间距 
               }  
               else { 
                   imageView = (ImageView) convertView; 
               } 
               if(!list.isEmpty())
               imageView.setImageBitmap(list.get(position));
               return imageView; 
       } 

}

加载中
0
寒情漠漠
寒情漠漠
使用bitmapfactory.options设置下图像 或者加Thumbnails 设缩略图
l
lovingPuff
我的list里面的bitmap不回收有问题的吗?
返回顶部
顶部