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

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

}

最佳答案

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

imageView.setImageBitmap(null);

关于android - 从 ImageView 中删除/移除当前图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326347/

相关文章:

user-interface - 为 erlang 编程获取 GUI 前端?

安卓。 ImageView 作为 RadioButton

安卓 ImageView

android - 背景图像减慢应用程序

java - Cordova 与 Ionic 无法检测到 Java 或 Android Studio

android - 如何设置在 ListPreference 中选中的值

java - 如何在 java 中禁用 javax.swing.JButton?

user-interface - 用户界面和用户体验有什么区别?

java - Box2d 内存泄漏

Android Room 插入测试失败