从ImageView中删除/删除当前图像?

时间:2011-11-30 13:00:17

标签: android user-interface imageview

每当用户再次单击将另一个图像设置为imageView时,我想从imageView中删除/删除或清除图像。我得到OutOfMemoryError:位图大小超过VM预算(堆大小= 7239KB,分配= 2769KB,位图大小= 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 个答案:

答案 0 :(得分:25)

将资源设置为0对我来说不起作用。以下确实有效:

imageView.setImageBitmap(null);

答案 1 :(得分:1)

使用特殊资源Id'0'。它不是有效的res id,因此图像被消隐。

答案 2 :(得分:1)

image.setImageDrawable(null);

将清除图像视图中的图像。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

viewToUse.setImageResource(android.R.color.transparent);
  • 我认为使用带有颜色标识符的setImageResource可以解决Android 2.2.1上的崩溃问题,请务必对其进行测试。

答案 5 :(得分:0)

edit_countflag.setBackgroundColor(0);