代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

哪种方法最适合重设位图的样式?

  •  1
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 15 年前

    我有一个页面,我显示的图像从画廊,然后我有下一页按钮移动到下一个活动上的按钮,我正在使用clearBitmap(); 释放它使用的内存。

    oncreate()
    {
    
     _image = (ImageView)findViewById(R.id.MyImage);
    
    _path = getRealPathFromURI(_data.getData());
    
    
                BitmapFactory.Options options = new BitmapFactory.Options();
    
    
                options.inSampleSize = 2;
    
                bitmap = BitmapFactory.decodeFile( _path, options );
    
    _image.setImageBitmap(bitmap);
    
    
    next.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View view) {
    
                Intent myIntent = new Intent(UploadImageOnly.this, ImageGallery.class);
    
                            startActivity(myIntent);
    
    clearBitmap();
    
    
    
    
    }
    

    这给了我一个错误 logcat公司

    08-13 12:07:07.649: ERROR/AndroidRuntime(753): Uncaught handler: thread main exiting due to uncaught exception
    08-13 12:07:07.869: ERROR/AndroidRuntime(753): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@43c13dd0
    08-13 12:07:07.869: ERROR/AndroidRuntime(753):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
    08-13 12:07:07.869: ERROR/AndroidRuntime(753):     at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
    08-13 12:07:07.869: ERROR/AndroidRuntime(753):     at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
    08-13 12:07:07.869: ERROR/AndroidRuntime(753):     at android.widget.ImageView.onDraw(ImageView.java:845)
    08-13 12:07:07.869: ERROR/AndroidRuntime(753):     at android.view.View.draw(View.java:6535)
    

    代码

    public void clearBitmap() {
    
            try{
                if(bitmap!=null && !bitmap.isRecycled()){
                        bitmap.recycle();
                        bitmap = null;
                }
                }catch(Throwable e){
                        System.out.println(e.getMessage());
                        e.printStackTrace();
                }
    
              System.gc();
    
             }
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Rich Schuler    15 年前

    就让垃圾收集器来处理吧。你不需要使用 Bitmap#recycle() 在你的情况下或任何正常情况下真的。