Android 压缩图片

版权声明:https://blog.****.net/qq_32425789/article/details/85052201

 直接上图

Android 压缩图片

 

原图信息:

Android 压缩图片
 

Android 压缩图片

压缩后:
Android 压缩图片
 

 

 

贴代码:

public class imageviewtool {
    private static final String TAG = "MainActivity";

    public static Bitmap decodeSampledBitmapResesouce(Resources res,int resId,int reqWidth,int reqHeight){
        final BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(res,resId,options);
        options.inSampleSize=ImageSize(options,reqWidth,reqHeight);
        options.inJustDecodeBounds=false;
        return BitmapFactory.decodeResource(res,resId,options);
    }

    public static int ImageSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
        final int height=options.outHeight;
        final int width=options.outWidth;
        int inSampleSize=1;

        if(height>reqHeight ||width>reqWidth){
            final int hanflHeight=height/2;
            final int hanflWidth=width/2;

            while (hanflHeight/inSampleSize>=reqHeight&&hanflWidth/inSampleSize>=reqWidth){
                inSampleSize*=2;
            }
        }
        return inSampleSize;
    }
}

 

虽然图片压缩网上一找一大把,不过还是有些要注意的地方: options.inJustDecodeBounds=true;