从ImageView中删除/移除当前图片?

9

每当用户再次点击设置图像到imageView时,我希望能够删除/移除或清除imageView中的图像。我遇到了OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7239KB, Allocated=2769KB, Bitmap Size=8748KB)的问题。

以下是我的代码:

ImageView imageView;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
Bitmap yourSelectedImage;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


}


@Override
protected void onResume() {
    super.onResume();
    imageView = (ImageView) findViewById(R.id.imageView);

            ((ImageView) findViewById(R.id.imageView))
    .setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            goToGallery();
        }
    });

}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            /*Toast.makeText(getBaseContext(), "" + selectedImagePath, 1000)
                    .show();
            *///editText2.setText(selectedImagePath);

            // Convert file path into bitmap image using below line.
            yourSelectedImage = BitmapFactory
                    .decodeFile(selectedImagePath);

            // put bitmapimage in your imageview
            imageView.setImageBitmap(yourSelectedImage);


        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
private void goToGallery()
{


    // in onCreate or any event where your want the user to
    // select a file
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(
            Intent.createChooser(intent, "Select Picture"),
            SELECT_PICTURE);

}
6个回答

28

将资源设置为0对我没有起作用。但是以下方法确实有效:

imageView.setImageBitmap(null);

1
image.setImageDrawable(null);

将清除ImageView中的图像。


1

使用特殊资源ID“0”。它不是有效的资源ID,因此图像被清空。


0

当您将另一张图片设置为当前的ImageView时,先前的图片不再用于ImageView。因此,无需进行额外的工作。


嘿,这是真的,但如果图像在另一个视图中没有显示出来,我该怎么办?我的问题在于ImageView中首先设置了图像,在按下下一个按钮后,同一图像设置在另一个图像中。所以我想清除第一张图片。请帮忙。 - Google

0
viewToUse.setImageResource(android.R.color.transparent);
  • 我认为在Android 2.2.1上使用带有颜色标识符的setImageResource会导致崩溃问题,一定要进行测试。

0
edit_countflag.setBackgroundColor(0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接