Suggestions to avoid bitmap Out of Memory error

前端 未结 6 1880
一向
一向 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条回答
  •  抹茶落季
    2020-11-30 02:59

    just use this function to decode...this is perfect solution for your error..because i also getting same error and i got this solution..

    public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
         try {
             //Decode image size
             BitmapFactory.Options o = new BitmapFactory.Options();
             o.inJustDecodeBounds = true;
             BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
             //The new size we want to scale to
             final int REQUIRED_WIDTH=WIDTH;
             final int REQUIRED_HIGHT=HIGHT;
             //Find the correct scale value. It should be the power of 2.
             int scale=1;
             while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                 scale*=2;
    
             //Decode with inSampleSize
             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize=scale;
             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
         } catch (FileNotFoundException e) {}
         return null;
     }
    

提交回复
热议问题