content: package com.afei.camera2getpreview.util;
  
  import android.graphics.Bitmap;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  public class FileUtil {
  
      // Android新的版本对SDCARD的权限管理更严格了，直接在该目录存bin文件可能没有权限
      public static final String SAVE_DIR = "/sdcard/DCIM/Camera2GetPreview/";
  
      public static boolean saveBytes(byte[] bytes, String imagePath) {
          File file = new File(imagePath);
          File parentFile = file.getParentFile();
          if (!parentFile.exists()) {
              parentFile.mkdirs();
          }
          try {
              FileOutputStream fos = new FileOutputStream(file);
              fos.write(bytes);
              fos.flush();
              fos.close();
              return true;
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return false;
      }
  
      public static boolean saveBitmap(Bitmap bitmap, String imagePath) {
          if (bitmap == null) {
              return false;
          }
          File file = new File(imagePath);
          File parentFile = file.getParentFile();
          if (!parentFile.exists()) {
              parentFile.mkdirs();
          }
          try {
              FileOutputStream fos = new FileOutputStream(file);
              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
              fos.flush();
              fos.close();
              return true;
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return false;
      }
  
  }
  