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); }
source share