FileUtil.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.mylove.okhttp;
  2. import android.graphics.Bitmap;
  3. import android.os.Environment;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. /**
  8. * @author myLove
  9. * @time 2017/11/24 13:55
  10. * @e-mail mylove.520.y@gmail.com
  11. * @overview
  12. */
  13. class FileUtil {
  14. /**
  15. * @param saveDir
  16. * @return
  17. * @throws IOException 判断下载目录是否存在
  18. */
  19. static String isExistDir(String saveDir) throws IOException {
  20. // 下载位置
  21. File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
  22. if (!downloadFile.mkdirs()) {
  23. downloadFile.createNewFile();
  24. }
  25. String savePath = downloadFile.getAbsolutePath();
  26. return savePath;
  27. }
  28. /**
  29. * 图片保存
  30. */
  31. static String saveImage(Bitmap bitmap, String path) {
  32. File file = new File(path);
  33. try {
  34. if (!file.exists()) {
  35. file.createNewFile();
  36. }
  37. FileOutputStream fos = new FileOutputStream(file);
  38. String substring = path.substring(path.length() - 3, path.length());
  39. if (substring.equals("png")) {
  40. bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  41. } else {
  42. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  43. }
  44. fos.flush();
  45. fos.close();
  46. } catch (Exception e) {
  47. e.fillInStackTrace();
  48. }
  49. return path;
  50. }
  51. static String getSDPath() {
  52. File sdDir = null;
  53. boolean sdCardExist = Environment.getExternalStorageState()
  54. .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
  55. if (sdCardExist) {//判断sd卡是否存在
  56. sdDir = Environment.getExternalStorageDirectory();//获取跟目录
  57. }
  58. return sdDir.toString();
  59. }
  60. }