package com.mylove.okhttp; import android.graphics.Bitmap; import android.os.Environment; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * @author myLove * @time 2017/11/24 13:55 * @e-mail mylove.520.y@gmail.com * @overview */ class FileUtil { /** * @param saveDir * @return * @throws IOException 判断下载目录是否存在 */ static String isExistDir(String saveDir) throws IOException { // 下载位置 File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir); if (!downloadFile.mkdirs()) { downloadFile.createNewFile(); } String savePath = downloadFile.getAbsolutePath(); return savePath; } /** * 图片保存 */ static String saveImage(Bitmap bitmap, String path) { File file = new File(path); try { if (!file.exists()) { file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); String substring = path.substring(path.length() - 3, path.length()); if (substring.equals("png")) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); } else { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } fos.flush(); fos.close(); } catch (Exception e) { e.fillInStackTrace(); } return path; } static String getSDPath() { File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist) {//判断sd卡是否存在 sdDir = Environment.getExternalStorageDirectory();//获取跟目录 } return sdDir.toString(); } }