Suggestions to avoid bitmap Out of Memory error

前端 未结 6 1894
一向
一向 2020-11-30 02:29

I am working on an android application. The application has a view containing lots of image. I had an error, I will try to give as much information as possible hoping someon

6条回答
  •  旧时难觅i
    2020-11-30 03:10

    Hi you have to decode the file . for this try with the following method.

      public static Bitmap new_decode(File f) {
    
            // decode image size
    
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            o.inDither = false; // Disable Dithering mode
    
            o.inPurgeable = true; // Tell to gc that whether it needs free memory,
                                    // the Bitmap can be cleared
    
            o.inInputShareable = true; // Which kind of reference will be used to
                                        // recover the Bitmap data after being
                                        // clear, when it will be used in the future
            try {
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 300;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 1.5 < REQUIRED_SIZE && height_tmp / 1.5 < REQUIRED_SIZE)
                    break;
                width_tmp /= 1.5;
                height_tmp /= 1.5;
                scale *= 1.5;
            }
    
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            // o2.inSampleSize=scale;
            o.inDither = false; // Disable Dithering mode
    
            o.inPurgeable = true; // Tell to gc that whether it needs free memory,
                                    // the Bitmap can be cleared
    
            o.inInputShareable = true; // Which kind of reference will be used to
                                        // recover the Bitmap data after being
                                        // clear, when it will be used in the future
            // return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            try {
    
    //          return BitmapFactory.decodeStream(new FileInputStream(f), null,
    //                  null);
                Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null);
                System.out.println(" IW " + width_tmp);
                System.out.println("IHH " + height_tmp);           
                   int iW = width_tmp;
                    int iH = height_tmp;
    
                   return Bitmap.createScaledBitmap(bitmap, iW, iH, true);
    
            } catch (OutOfMemoryError e) {
                // TODO: handle exception
                e.printStackTrace();
                // clearCache();
    
                // System.out.println("bitmap creating success");
                System.gc();
                return null;
                // System.runFinalization();
                // Runtime.getRuntime().gc();
                // System.gc();
                // decodeFile(f);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
    
        }
    

提交回复
热议问题