Delete/remove current image from ImageView?

24,169

Solution 1

Setting a resource to 0 did not work for me. The following did work though:

imageView.setImageBitmap(null);

Solution 2

image.setImageDrawable(null);

will clear the image in the imageview.

Solution 3

use the special resource Id '0'. It is not a valid res id, hence the image is blanked.

Share:
24,169
Vikasdeep Singh
Author by

Vikasdeep Singh

Updated on August 04, 2022

Comments

  • Vikasdeep Singh
    Vikasdeep Singh almost 2 years

    I want to delete/remove or clear image from imageView every time when user again click to set another image to the imageView. i am getting OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7239KB, Allocated=2769KB, Bitmap Size=8748KB) here is my code:

    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);
    
    }
    
  • Google
    Google over 11 years
    hey its true but if image is not coming in the another view then what can i do? my problem is in imageview first image is set,after press next button same image is set in the another image.so i wanaa clear that first image.plz help