Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

android / dayu

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • dayu
  • ..
  • util
  • FileUtil.java
Find file
Normal viewHistoryPermalink
FileUtil.java 2 KB
Newer Older
mReturn's avatar
学习模块功能开发
d7f9680c
 
mReturn committed 5 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package com.cjt2325.cameralibrary.util;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * =====================================
 * 作    者: 陈嘉桐
 * 版    本:1.1.4
 * 创建日期:2017/4/25
 * 描    述:
 * =====================================
 */
public class FileUtil {
    private static final String TAG = "CJT";
    private static String storagePath = "";
    private static String DST_FOLDER_NAME = "JCamera";

wukun's avatar
0325
aa1f19b1
 
wukun committed a year ago
24
    private static String initPath(String basePath) {
mReturn's avatar
学习模块功能开发
d7f9680c
 
mReturn committed 5 years ago
25
        if (storagePath.equals("")) {
wukun's avatar
0325
aa1f19b1
 
wukun committed a year ago
26
            storagePath = basePath + File.separator + DST_FOLDER_NAME;
mReturn's avatar
学习模块功能开发
d7f9680c
 
mReturn committed 5 years ago
27 28 29 30 31 32 33 34
            File f = new File(storagePath);
            if (!f.exists()) {
                f.mkdir();
            }
        }
        return storagePath;
    }

wukun's avatar
0325
aa1f19b1
 
wukun committed a year ago
35
    public static String saveBitmap(String dir, Bitmap b,String basePath) {
mReturn's avatar
学习模块功能开发
d7f9680c
 
mReturn committed 5 years ago
36
        DST_FOLDER_NAME = dir;
wukun's avatar
0325
aa1f19b1
 
wukun committed a year ago
37
        String path = initPath(basePath);
mReturn's avatar
学习模块功能开发
d7f9680c
 
mReturn committed 5 years ago
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        long dataTake = System.currentTimeMillis();
        String jpegName = path + File.separator + "picture_" + dataTake + ".jpg";
        try {
            FileOutputStream fout = new FileOutputStream(jpegName);
            BufferedOutputStream bos = new BufferedOutputStream(fout);
            b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return jpegName;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean deleteFile(String url) {
        boolean result = false;
        File file = new File(url);
        if (file.exists()) {
            result = file.delete();
        }
        return result;
    }

    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }
}