开发者

which is the suitable method to recyle bitmap?

开发者 https://www.devze.com 2023-01-11 15:34 出处:网络
friends, i have a page on which i am displaying image from gallery then i have next page button to move to next activity on that button i am using clearBitmap();

friends,

i have a page on which i am displaying image from gallery then i have next page button to move to next activity on that button i am using clearBitmap(); to free memory used by it.

private Bitmap bitmap;

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开发者_Python百科 onClick(View view) {

            Intent myIntent = new Intent(UploadImageOnly.this, ImageGallery.class);

                        startActivity(myIntent);

clearBitmap();




}

which gives me error 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)

code

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

         }


Just let the garbage collector handle it. You do not need to use Bitmap#recycle() in your case or in any normal case really.

0

精彩评论

暂无评论...
验证码 换一张
取 消