riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cn.flightfeather.thirdapp.util.file;
 
import android.content.Context;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import cn.flightfeather.thirdapp.bean.entity.Mediafile;
 
public class FileUtil {
 
    //复制文件
    public static void copyFile(File oldfile, File newfile) throws IOException {
        FileInputStream ins = new FileInputStream(oldfile);
        FileOutputStream out = new FileOutputStream(newfile);
        //自定义缓冲对象
        byte[] b = new byte[1024];
        int n = 0;
        while ((n = ins.read(b)) != -1) {
            out.write(b, 0, b.length);
        }
        ins.close();
        out.close();
 
        System.out.println("copy success");
    }
 
    /**
     * 根据路径获取本机的图片
     * @param mediafile
     * @return
     */
    public static File getFileFromMediaFile(Mediafile mediafile){
        File file = new File(Environment.getExternalStorageDirectory(), (mediafile.getPath() + mediafile.getDescription()));
        return file;
    }
 
    //将图片写入磁盘
    public static File writeResponseBodyToDisk(Context context, File file, Mediafile mediafile) {
        try {
            InputStream is = new FileInputStream(file);
            File osFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),(mediafile.getPath() + mediafile.getDescription()));
            if (osFile.exists()) {
                osFile.delete();
                osFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),(mediafile.getPath() + mediafile.getDescription()) );
            }
            osFile.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(osFile);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fos.close();
            bis.close();
            is.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    //将图片插入到系统图库
    public static String sendMediaScanBroadcast(Context context,  File file) {
        try {
            String url = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null);
            String[] paths = new String[]{file.getAbsolutePath()};
            MediaScannerConnection.scanFile(context, paths, null, null);
//            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//            Uri uri = Uri.fromFile(file);
//            intent.setData(uri);
//            context.sendBroadcast(intent);
            return url;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                String id = contentUri.getLastPathSegment().split(":")[1];
//                final String[] imageColumns = {MediaStore.Images.Media.DATA };
//                final String imageOrderBy = null;
//                Cursor imageCursor = context.getContentResolver().query(contentUri, imageColumns,
//                        MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
//                if (imageCursor.moveToFirst()) {
//                    return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
//                }else{
//                    return null;
//                }
//            } else {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return "";
    }
}