将位图添加到数组中,然后在GridView内的ImageView中设置它

时间:2015-11-19 16:40:23

标签: android gridview arraylist bitmap imageview

由于在使用手机摄像头拍摄的照片被添加到GridView内的imageView时出现内存不足错误,我尝试重新缩放它 - 作为位图(这很好用)。我现在想要将图像添加到一个数组中,项目将从GridViews ImageView中加载。

我尝试了什么:

public class ImageGridAdapter extends BaseAdapter {


public Uri imageUri;
public String picture;
ArrayList<Bitmap> bitmapArrayList = new ArrayList<Bitmap>();


private Context mContext;

public ImageGridAdapter(Context c) {

    this.mContext = c;
}

@Override
public int getCount() {
        return bitmapArrayList.size();
}

@Override
public Object getItem(int position) {

    return position;
}

@Override
public long getItemId(int position) {

    return position;
}


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

    try {
        imageUri = PictureGroupActivity.selectedImage;
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = mContext.getContentResolver().query(imageUri, filePathColumn, null, null, null);

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

        picture = cursor.getString(columnIndex);
        cursor.close();
    } catch (Exception e) {}


    bitmapArrayList.add(BitmapScaled(picture, 100, 100));
    Toast.makeText(mContext.getApplicationContext(), "bitmapArrayList je: " + bitmapArrayList, Toast.LENGTH_SHORT).show();


    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 5, 5, 5);
    }  else {
        imageView = (ImageView) convertView;
    }

        imageView.setImageBitmap(bitmapArrayList.get(position));

    return imageView;
}

private Bitmap BitmapScaled(String picturePath, int width, int height) {
    BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
    sizeOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, sizeOptions);

    sizeOptions.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(picturePath, sizeOptions);

}
}

将适配器设置为GridView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_group_activity_layout);

    GridView gridView = (GridView) findViewById(R.id.picture_group_gridView);
    gridView.setAdapter(new ImageGridAdapter(this));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(PictureGroupActivity.this, "You clicked " + position, Toast.LENGTH_SHORT).show();
        }
    });
}

选择图片:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    String picturePath = loadPath();

    if (id == R.id.addPictureFolder) {
        if (picturePath != null) {
            Toast.makeText(this, "Click the plus button to add pictures", Toast.LENGTH_LONG).show();

            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        } else {

            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    } else if (id == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    gridView = (GridView)findViewById(R.id.picture_group_gridView);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        selectedImage = data.getData();

        Intent restart = getIntent();
        finish();
        startActivity(restart);

    }
}

这没什么,没有错误,没有显示图像,没有。

我做错了什么? (如果我不在答案的附近,你怎么做?) 提前谢谢!

1 个答案:

答案 0 :(得分:0)

好像你没有在活动中设置适配器中的arraylist。 bitmapArrayList大小为零,这就是gridview没有显示任何内容的原因。请检查bitmapArrayList大小。