【问题标题】:Delete/remove current image from ImageView?从 ImageView 中删除/删除当前图像?
【发布时间】:2018-05-21 20:17:31
【问题描述】:

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

}

【问题讨论】:

    标签: android user-interface imageview


    【解决方案1】:

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

    imageView.setImageBitmap(null);
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        image.setImageDrawable(null);
        

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 嘿,这是真的,但如果图像没有出现在另一个视图中,那么我该怎么办?我的问题是在 imageview 中设置了第一个图像,按下下一个按钮后,在另一个图像中设置了相同的图像。所以我想清楚第一个图像。请帮助
          【解决方案5】:
          viewToUse.setImageResource(android.R.color.transparent);
          
          • 我认为使用带有颜色标识符的 setImageResource 会给您在 Android 2.2.1 上带来崩溃问题,请务必对其进行测试。

          【讨论】:

            【解决方案6】:
            edit_countflag.setBackgroundColor(0);
            

            【讨论】:

              猜你喜欢
              • 2016-08-29
              • 1970-01-01
              • 2011-10-02
              • 1970-01-01
              • 2011-03-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-10-20
              相关资源
              最近更新 更多