riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
app/src/main/java/cn/flightfeather/thirdappmodule/task/CompressPhotoTask.java
@@ -47,38 +47,42 @@
            String fileName = mediafile.getGuid()+".jpg";
            File outputFile = new File(Environment.getExternalStorageDirectory(),path+fileName);
            outputFile.getParentFile().mkdirs();
            BitmapFactory.Options opts = new BitmapFactory.Options();
            //将图片的长宽缩小为1/2
            opts.inSampleSize = 2;
            Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(),opts);
            Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
            int aa = bm.getHeight();
            int bb = bm.getWidth();
            Log.i("bitmap",bm.getHeight()+" "+bm.getWidth());
            Log.i("bitmap", bm.getHeight() + " " + bm.getWidth());
            //压缩缩略图
            Bitmap bmSmall = null;
            int normalSize = 1440;
            int width,height = 0;
            if (bm.getWidth()>=bm.getHeight()){
            int width, height = 0;
            if (bm.getWidth() >= bm.getHeight()) {
                width = normalSize;
                height = normalSize*bm.getHeight()/bm.getWidth();
            }else {
                height =normalSize;
                width = normalSize*bm.getWidth()/bm.getHeight();
                height = normalSize * bm.getHeight() / bm.getWidth();
            } else {
                height = normalSize;
                width = normalSize * bm.getWidth() / bm.getHeight();
            }
            if (bm!=null){
                bmSmall= Bitmap.createScaledBitmap(bm,width,height,true);
            if (bm != null) {
                bmSmall = Bitmap.createScaledBitmap(bm, width, height, true);
                try {
                    bmSmall.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(outputFile.getAbsolutePath()));
                    Log.i("compress success",outputFile.getAbsolutePath());
                    // 当图片大于1MB时,才会进行压缩处理
                    if (file.length() > 1024L * 1024) {
                        bmSmall.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(outputFile.getAbsolutePath()));
                    } else {
                        bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(outputFile.getAbsolutePath()));
                    }
                    Log.i("compress success", outputFile.getAbsolutePath());
                    outputFIleList.add(outputFile);
                        bm.recycle();
                        bm=null;
                    bm.recycle();
                    bmSmall.recycle();
                    bmSmall =null;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Log.i("compress failure",outputFile.getAbsolutePath());
                    Log.i("compress failure", outputFile.getAbsolutePath());
                }
            }
        }