Android png images big in memory

后端 未结 2 1819
北荒
北荒 2020-12-20 07:29

I have an application in android that has an imageflipper. Problem is, after about 8 images loaded to memory, I get an out of memory error.

Well, I tried to do dyna

相关标签:
2条回答
  • 2020-12-20 08:28

    Use BitmapFactory.Options

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opt.inSampleSize = 2;
    //this will decrease bitmap size,
    // but also affect quality of the image, 
    //so just play with this value to spot the good one;
    Bitmap b = BitmapFactory.decodeFile(fileName, opts);
    
    0 讨论(0)
  • 2020-12-20 08:34

    I used the below code snippet to over the memory related issues, We can over come this problem by the help of isRecycled() method. Add this code with yours, here finalImage is my BitmapDrawable and R.id.image_viewer is my imageview, you can change it yours

     @Override
            protected void onDestroy() {
    
                 finalImage.setCallback(null);
                 if (!((BitmapDrawable) finalImage).getBitmap().isRecycled()) {
                 ((BitmapDrawable) finalImage).getBitmap().recycle();
                 }
                 finalImage = null;
                unbindDrawables(findViewById(R.id.image_viewer));
                Runtime.getRuntime().gc();
                // scaledBitmap.recycle();
                System.gc();
                super.onDestroy();
            }
    
            private void unbindDrawables(View view) {
                if (view.getBackground() != null) {
                    view.getBackground().setCallback(null);
                }
                if (view instanceof ViewGroup) {
                    for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                        unbindDrawables(((ViewGroup) view).getChildAt(i));
                    }
                    ((ViewGroup) view).removeAllViews();
                }
            }
    
    0 讨论(0)
提交回复
热议问题