How to avoid OutOfMemory ex while rotating the image?

前端 未结 2 1086
醉酒成梦
醉酒成梦 2021-01-18 04:23
public static boolean rotateBitmapByExifAndSave(File targetFile){

  if (targetFile==null || !targetFile.exists() || !targetFile.canRead() || !targetFile.canWrite())         


        
相关标签:
2条回答
  • 2021-01-18 04:50

    Make a method name decode file:

    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;
         }
    

    then call this method like this (You can call this method in button click listener)

    Bitmap bi = decodeFile(new File(path),1280,800);
    

    Where path is the path of image where you save your image.. in my case it is

    String path = Environment.getExternalStorageDirectory().toString() + "/nature.jpg";
    

    In case of any problem - ask :) Hope this helps.

    0 讨论(0)
  • 2021-01-18 04:56

    Since there was no answer I assume there is no answer or maybe I just had asked the question a bit incorrectly. It looks like the only option here is to increase the app's heap size
    UPDATE:
    There is also another option - to work with bitmaps via NDK/JNI like here or to use Android Image-Magic lib. The Image Magic lib is pretty cool, to rotate an image all you need is:

    ImageInfo imageInfo = new ImageInfo(imageFile.getAbsolutePath());
    MagickImage magickImage = new MagickImage(imageInfo);
    magickImage.setCompression(100); // to minimize loss
    magickImage.rotateImage(90.0f).writeImage(imageInfo);
    

    MagickImage has many other image manipulating options as well. Blur, matte, scale, charcoal and many more. However its libraries size is noticable. Authors made a great job and they covered all possible plaforms: arm64-v8a, armeabi, armeabi-v7a, mips, mips64, x86, x86_64 and final size of all these libs is over 36Mb. So you should think before adding all the libs into one apk, maybe packaging 6 different versions using manifest to filter by chipset/platform is the right way.
    UPDATE
    Another option is to convert Immutable Bitmap into Mutable (wrap bitmaps into MappedByteBuffer)

    0 讨论(0)
提交回复
热议问题