2011-11-30 140 views
4

每次用戶再次單擊以將另一圖像設置爲imageView時,我想要從imageView中刪除/清除或清除圖像。我得到的OutOfMemoryError:位圖大小超過VM預算(堆大小= 7239KB,分配= 2769KB,位圖大小= 8748KB) 這裏是我的代碼:從ImageView刪除/刪除當前圖像?

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

} 

回答

20

將資源設置爲0對我無效。下面沒有工作,雖然:

imageView.setImageBitmap(null); 
0

當您另一幅圖像設置爲當前ImageView前一個不再用於ImageView。因此,不需要做額外的工作。

+0

嘿,它的真實性,但如果圖像不是在另一種觀點,那我該怎麼辦?我的問題是在imageview第一個圖像設置,按下一個按鈕後,相同的圖像設置在另一個圖像。所以我wanaa清除第一個image.plz幫助 – Google

1

使用特殊資源Id'0'。它不是一個有效的資源ID,因此圖像是空白的。

0
viewToUse.setImageResource(android.R.color.transparent); 
  • 我認爲使用setImageResource用顏色標識會給你在Android 2.2.1崩潰的問題,一定要進行測試。
1
image.setImageDrawable(null); 

將清除imageview中的圖像。