Delete / delete current image from ImageView?

I want to remove / delete or clear an image from imageView every time the user clicks again to set another image to the image. I get OutOfMemoryError: the size of the bitmap exceeds the VM budget (heap size = 7239 KB, allocated = 2769 KB, bitmap size = 8748 KB) 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); } 
+4
source share
5 answers

Setting the resource to 0 did not help me. The following has been completed:

 imageView.setImageBitmap(null); 
+20
source

use the special resource identifier '0'. This is not a valid res id, so the image is not full.

+1
source
 image.setImageDrawable(null); 

will clear the image in the image view.

+1
source

When you set another image to the current ImageView , the previous one is no longer used for ImageView . Therefore, there is no need to do extra work.

0
source
 viewToUse.setImageResource(android.R.color.transparent); 
  • I think using setImageResource with color id will give you error problems on Android 2.2.1, be sure to check it out.
0
source

Source: https://habr.com/ru/post/1383874/


All Articles