将图像从SD卡加载到网格视图会导致位图超出VM预算

时间:2012-12-06 06:46:11

标签: android-gridview bitmapimage image-loading

在我的应用程序即时加载来自SD卡的图像,在网格视图中将近40到50个图像用于此i创建的适配器,其中构造函数将图像从SD卡加载到位图的数组列表的全局变量中,在get视图中从数组返回位图列表问题来自此活动用户将移动到其他活动,删除后他将删除图像用户将返回网格活动并且必须再次加载适配器这会创建“位图超过预算”。所以我不知道这种方式从SD卡加载图像是否有任何更好的方法,任何库可用于从SD卡加载图像而没有内存泄漏

public class PicAdapter extends BaseAdapter {


    int defaultItemBackground;


    private Context galleryContext;


    Bitmap placeholder;


    public PicAdapter(Context c) {

        // instantiate context
        galleryContext = c;

        // create bitmap array
        bitmaplist=new ArrayList<Bitmap>();
        GlobalData.imageBitmaps = new WeakReference<ArrayList<Bitmap>>(bitmaplist);
        imageDescription = new ArrayList<String>();
        imagetitle = new ArrayList<String>();


        picturesCursor.moveToFirst();
        int i = 0;
        for (i = 0; i < picturesCursor.getCount(); i++) {



            String imagepath = picturesCursor.getString(picturesCursor
                    .getColumnIndex("img"));
            File cacheimage=new File(imagepath);

            if(!cacheimage.exists())
            {
                dBopenHelper.deleteHappyMoments(imagepath);

            }
            else
            {
            Bitmap bitmap = 
                    ViewUtils.decodeSampledBitmapFromResource(imagepath,
                            AppConstants.FRAME_WIDTH,
                            AppConstants.FRAME_HEIGHT);

            ViewUtils.recycleBitmapFrame();
            GlobalData.imageBitmaps.get().add(bitmap);
            imageDescription.add(picturesCursor.getString(picturesCursor
                    .getColumnIndex("image_description")));
            imagetitle.add(picturesCursor.getString(picturesCursor
                    .getColumnIndex("image_title")));
            picturesCursor.moveToNext();
            }

        }



        if (i == picturesCursor.getCount()) {


            Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.add_new);
            GlobalData.imageBitmaps.get().add(bitmap);
            imageDescription.add(
                    "Click Add to new Pictures..............");
        }

        // get the styling attributes - use default Andorid system resources

    }

    // BaseAdapter methods

    // return number of data items i.e. bitmap images
    public int getCount() {

        return bitmaplist.size();
    }

    // return item at specified position
    public Object getItem(int position) {
        return position;
    }

    // return item ID at specified position
    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = (ImageView) getLayoutInflater().inflate(
                    R.layout.item_grid_image, null);

            convertView.setLayoutParams(new GridView.LayoutParams(
                    AppConstants.FRAME_WIDTH, AppConstants.FRAME_HEIGHT));


        }
        galleryImageView = (ImageView) convertView;


        galleryImageView.setImageBitmap(GlobalData.imageBitmaps.get().get(position));

    public void addPic(Bitmap newPic) {
        // set at currently selected index
        GlobalData.imageBitmaps.get().add(currentPic, newPic);
    }

    // return bitmap at specified position for larger display
    public Bitmap getPic(int posn) {
        // return bitmap at posn index
        return GlobalData.imageBitmaps.get().get(posn);
    }
}

示例代码帮助我很多

提前致谢

1 个答案:

答案 0 :(得分:0)

  1. 如果您的图片尺寸较大,则需要使用某种队列逐个显示它们。
  2. 确保正确解码它们。