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

发布于 2024-12-18 21:50:01 字数 2192 浏览 2 评论 0原文

每次当用户再次单击将另一个图像设置到 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);

}

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

多情出卖 2024-12-25 21:50:01

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

imageView.setImageBitmap(null);

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

imageView.setImageBitmap(null);
平生欢 2024-12-25 21:50:01

使用特殊资源 ID“0”。它不是有效的 res id,因此图像为空白。

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

梦初启 2024-12-25 21:50:01
image.setImageDrawable(null);

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

image.setImageDrawable(null);

will clear the image in the imageview.

金橙橙 2024-12-25 21:50:01

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

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

挖个坑埋了你 2024-12-25 21:50:01
viewToUse.setImageResource(android.R.color.transparent);
  • 我认为在 Android 2.2.1 上使用带有颜色标识符的 setImageResource 会导致崩溃问题,请务必对其进行测试。
viewToUse.setImageResource(android.R.color.transparent);
  • I think using setImageResource with a color identifier will give you crashing issues on Android 2.2.1, make sure to test it.
腹黑女流氓 2024-12-25 21:50:01
edit_countflag.setBackgroundColor(0);
edit_countflag.setBackgroundColor(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文