| package com.doumee.lib_coremodel.util; | 
|   | 
| import android.content.Context; | 
| import android.graphics.Bitmap; | 
| import android.graphics.BitmapFactory; | 
| import android.os.Build; | 
| import android.os.Environment; | 
| import android.util.Log; | 
|   | 
| import java.io.BufferedOutputStream; | 
| import java.io.ByteArrayInputStream; | 
| import java.io.ByteArrayOutputStream; | 
| import java.io.File; | 
| import java.io.FileOutputStream; | 
| import java.io.IOException; | 
| import java.sql.Timestamp; | 
| import java.text.DecimalFormat; | 
|   | 
| import timber.log.Timber; | 
|   | 
| public class PictureUtil { | 
|   | 
|     /** | 
|      * 质量压缩方法 | 
|      * @param image | 
|      * @return | 
|      */ | 
|     public static Bitmap compressImage(Bitmap image) { | 
|         //Bitmap image = BitmapFactory.decodeFile(filePath,getBitmapOption(2)); | 
|         ByteArrayOutputStream baos = new ByteArrayOutputStream(); | 
|         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 | 
|         int options = 90; | 
|         while (baos.toByteArray().length / 1024 > 100) { // 循环判断若是压缩后图片是否大于100kb,大于继续压缩 | 
|             baos.reset(); // 重置baos即清空baos | 
|             image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中 | 
|             options -= 10;// 每次都减小10 | 
|         } | 
|         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中 | 
|         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片 | 
|         return bitmap; | 
|     } | 
|   | 
|     private static BitmapFactory.Options getBitmapOption(int inSampleSize){ | 
|         System.gc(); | 
|         BitmapFactory.Options options = new BitmapFactory.Options(); | 
|         options.inPurgeable = true; | 
|         options.inSampleSize = inSampleSize; | 
|         return options; | 
|     } | 
|   | 
|     public static File saveBitmapFile(Context context,Bitmap bitmap){ | 
|         Timber.tag("Compressor").d("压缩中大小:"+bitmap.getByteCount()/1024,""); | 
|         //Bitmap b= sharpenImageAmeliorate(bitmap); | 
|         File file= new File(setMkdir(context), System.currentTimeMillis()+".jpg");//将要保存图片的路径 | 
|         try { | 
|             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); | 
|             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); | 
|             bos.flush(); | 
|             bos.close(); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return file; | 
|     } | 
|   | 
|     /** | 
|      * 图片按比例大小压缩方法 | 
|      * @param filePath (根据Bitmap图片压缩) | 
|      * @return | 
|      */ | 
|     public static File compressScale(Context context,String  filePath) { | 
|         Bitmap image = BitmapFactory.decodeFile(filePath,getBitmapOption(2)); | 
|         ByteArrayOutputStream baos = new ByteArrayOutputStream(); | 
|         image.compress(Bitmap.CompressFormat.JPEG, 100, baos); | 
|         // 判断若是图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 | 
|         if (baos.toByteArray().length / 1024 > 1024) { | 
|             baos.reset();// 重置baos即清空baos | 
|             image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 这里压缩50%,把压缩后的数据存放到baos中 | 
|         } | 
|         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); | 
|         BitmapFactory.Options newOpts = new BitmapFactory.Options(); | 
|         // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 | 
|         newOpts.inJustDecodeBounds = true; | 
|         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); | 
|         newOpts.inJustDecodeBounds = false; | 
|         int w = newOpts.outWidth; | 
|         int h = newOpts.outHeight; | 
|         Log.i("", w + "---------------" + h); | 
|         // 如今主流手机比较可能是800*480分辨率,因此高和宽咱们设置为 | 
|         // float hh = 800f;// 这里设置高度为800f | 
|         // float ww = 480f;// 这里设置宽度为480f | 
|         float hh = 1080f; | 
|         float ww = 1080f; | 
|         // 缩放比。因为是固定比例缩放,只用高或者宽其中一个数据进行计算便可 | 
|         int be = 1;// be=1表示不缩放 | 
|         if (w > h && w > ww) {// 若是宽度大的话根据宽度固定大小缩放 | 
|             be = (int) (newOpts.outWidth / ww); | 
|         } else if (w < h && h > hh) { // 若是高度高的话根据高度固定大小缩放 | 
|             be = (int) (newOpts.outHeight / hh); | 
|         } | 
|         if (be <= 0) { | 
|             be = 1; | 
|         } | 
|         newOpts.inSampleSize = be; // 设置缩放比例 | 
|         // newOpts.inPreferredConfig = Config.RGB_565;//下降图片从ARGB888到RGB565 | 
|         // 从新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 | 
|         isBm = new ByteArrayInputStream(baos.toByteArray()); | 
|         bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); | 
|         return saveBitmapFile(context,compressImage(bitmap));// 压缩比如例大小后再进行质量压缩 | 
|         //return bitmap; | 
|     } | 
|   | 
|     /** | 
|      * 获取目录名称 | 
|      * | 
|      * @param url | 
|      * @return FileName | 
|      */ | 
|     public static String getFileName(String url) { | 
|         int lastIndexStart = url.lastIndexOf("/"); | 
|         if (lastIndexStart != -1) { | 
|             return url.substring(lastIndexStart + 1, url.length()); | 
|         } else { | 
|             return new Timestamp(System.currentTimeMillis()).toString(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 文件是否存在 | 
|      * | 
|      * @param path | 
|      * @return | 
|      */ | 
|     public static boolean FileIsExist(String path) { | 
|         File file = new File(path); | 
|   | 
|         return file.exists(); | 
|     } | 
|   | 
|     /** | 
|      * 判断SD卡是否存在 | 
|      * | 
|      * @return boolean | 
|      */ | 
|     public static boolean checkSDCard() { | 
|         if (Environment.getExternalStorageState().equals( | 
|                 Environment.MEDIA_MOUNTED)) { | 
|             return true; | 
|         } else { | 
|             return false; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 保存文件到指定目录 | 
|      * | 
|      * @param context | 
|      * @return 文件保存的目录 | 
|      */ | 
|     public static String setMkdir(Context context) { | 
|         String filePath = null; | 
|         if (checkSDCard()) { | 
|             filePath = Environment.getExternalStorageDirectory() | 
|                     + File.separator + "com.wei.mymvvm" + File.separator | 
|                     + "images"; | 
|         } else { | 
|             filePath = context.getCacheDir().getAbsolutePath() + File.separator | 
|                     + "com.wei.mymvvm" + File.separator + "images"; | 
|         } | 
|         File file = new File(filePath); | 
|         if (!file.exists()) { | 
|             file.mkdirs(); | 
|             Log.e("file", "目录不存在   创建目录    "); | 
|         } else { | 
|             Log.e("file", "目录存在"); | 
|         } | 
|         return filePath; | 
|     } | 
|   | 
|     public static String getFile(Context context){ | 
|         File sdDir = null; | 
|         boolean sdCardExist = Environment.getExternalStorageState().equals( | 
|                 Environment.MEDIA_MOUNTED);// 判断sd卡是否存在 | 
|         if (sdCardExist) { | 
|             if (Build.VERSION.SDK_INT>=29){ | 
|                 //Android10之后 | 
|                 sdDir = context.getExternalFilesDir(null); | 
|             }else { | 
|                 sdDir = Environment.getExternalStorageDirectory();// 获取SD卡根目录 | 
|             } | 
|         } else { | 
|             sdDir = Environment.getRootDirectory();// 获取跟目录 | 
|         } | 
|         return sdDir.toString(); | 
|     } | 
|   | 
|     /** | 
|      * 获取路径 | 
|      * | 
|      * @return | 
|      * @throws IOException | 
|      */ | 
|     public static String getPath(Context context, String url) { | 
|         String path = null; | 
|         try { | 
|             path = getFile(context) + File.separator | 
|                     + "shixun.apk"; | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return path; | 
|     } | 
|   | 
|     public static long getFileSize(File f) throws Exception// 取得文件夹大小 | 
|     { | 
|         long size = 0; | 
|         File flist[] = f.listFiles(); | 
|         if (flist != null) { | 
|             for (int i = 0; i < flist.length; i++) { | 
|                 if (flist[i].isDirectory()) { | 
|                     size = size + getFileSize(flist[i]); | 
|                 } else { | 
|                     size = size + flist[i].length(); | 
|                 } | 
|             } | 
|         } | 
|         return size; | 
|     } | 
|   | 
|     /** | 
|      * 获取文件的大小 | 
|      * | 
|      * @param fileS | 
|      *            文件的大小 | 
|      * @return | 
|      */ | 
|     public static String FormetFileSize(long fileS) {// 转换文件大小 | 
|         DecimalFormat df = new DecimalFormat("0.0");// #.00 | 
|         String fileSizeString = ""; | 
|         if (fileS < 1024) { | 
|             if (fileS < 70) { | 
|                 fileSizeString = "0B"; | 
|             } else { | 
|                 fileSizeString = df.format((double) fileS) + "B"; | 
|             } | 
|         } else if (fileS < 1048576) { | 
|             fileSizeString = df.format((double) fileS / 1024) + "K"; | 
|         } else if (fileS < 1073741824) { | 
|             fileSizeString = df.format((double) fileS / 1048576) + "M"; | 
|         } else { | 
|             fileSizeString = df.format((double) fileS / 1073741824) + "G"; | 
|         } | 
|         return fileSizeString; | 
|     } | 
|   | 
|     /** | 
|      * 删除该目录下的指定文件 | 
|      * | 
|      * @param filePath | 
|      *            文件目录 | 
|      * @param fileName | 
|      *            文件名 | 
|      * @return | 
|      */ | 
|     public static boolean deleteFile(String filePath, String fileName) { | 
|         if (FileIsExist(filePath + fileName)) { | 
|             File file = new File(filePath + fileName); | 
|   | 
|             return file.delete(); | 
|         } | 
|   | 
|         return true; | 
|     } | 
|   | 
|     /** | 
|      * 删除该路径指定的文件 | 
|      * | 
|      * @param path | 
|      *            文件路径 | 
|      * @return | 
|      */ | 
|     public static boolean deleteFile(String path) { | 
|         if (FileIsExist(path)) { | 
|             File file = new File(path); | 
|             return file.delete(); | 
|         } | 
|         return true; | 
|     } | 
|   | 
|     /** | 
|      * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 | 
|      * | 
|      * @param file | 
|      */ | 
|   | 
|     public static void deleteFilesByDirectory(File file) { | 
|         if (file.isFile()) { | 
|             file.delete(); | 
|             return; | 
|         } | 
|   | 
|         if (file.isDirectory()) { | 
|             File[] childFiles = file.listFiles(); | 
|             if (childFiles == null || childFiles.length == 0) { | 
|                 file.delete(); | 
|                 return; | 
|             } | 
|   | 
|             for (int i = 0; i < childFiles.length; i++) { | 
|                 deleteFilesByDirectory(childFiles[i]); | 
|             } | 
|             file.delete(); | 
|         } | 
|     } | 
| } |