lmw
2023-06-07 f9dd2cdac746d308d5c4bcfdbea389ab67a66b12
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.luck.picture.lib.tools;
 
import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
 
import androidx.annotation.Nullable;
 
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
 
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.OutputStream;
 
/**
 * @author:luck
 * @date:2019-11-08 19:25
 * @describe:Android Q相关处理类
 */
public class AndroidQTransformUtils {
 
    /**
     * 解析Android Q版本下视频
     * #耗时操作需要放在子线程中操作
     *
     * @param ctx
     * @param path
     * @param customFileName
     * @param mineType
     * @return
     */
    public static String parseVideoPathToAndroidQ(Context ctx, String path, String customFileName, String mineType) {
        try {
            String suffix = PictureMimeType.getLastImgSuffix(mineType);
            String filesDir = PictureFileUtils.getVideoDiskCacheDir(ctx.getApplicationContext());
            ParcelFileDescriptor parcelFileDescriptor =
                    ctx.getContentResolver().openFileDescriptor(Uri.parse(path), "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            FileInputStream inputStream = new FileInputStream(fileDescriptor);
            String md5Value = Digest.computeToQMD5(inputStream);
            String fileName;
            if (!TextUtils.isEmpty(md5Value)) {
                fileName = TextUtils.isEmpty(customFileName) ? new StringBuffer().append("VID_").append(md5Value.toUpperCase()).append(suffix).toString() : customFileName;
            } else {
                fileName = TextUtils.isEmpty(customFileName) ? DateUtils.getCreateFileName("VID_") + suffix : customFileName;
            }
            if (filesDir != null) {
                String newPath = new StringBuffer().append(filesDir).append(File.separator).append(fileName).toString();
                File outFile = new File(newPath);
                if (outFile.exists()) {
                    return newPath;
                }
                boolean copyFileSuccess = PictureFileUtils.copyFile(inputStream, outFile);
                if (copyFileSuccess) {
                    return newPath;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 解析Android Q版本下图片
     * #耗时操作需要放在子线程中操作
     *
     * @param ctx
     * @param path
     * @param customFileName
     * @param mineType
     * @return
     */
    public static String parseImagePathToAndroidQ(Context ctx, String path, String customFileName, String mineType) {
        try {
            String suffix = PictureMimeType.getLastImgSuffix(mineType);
            String filesDir = PictureFileUtils.getDiskCacheDir(ctx.getApplicationContext());
            ParcelFileDescriptor parcelFileDescriptor =
                    ctx.getContentResolver().openFileDescriptor(Uri.parse(path), "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            FileInputStream inputStream = new FileInputStream(fileDescriptor);
            String md5Value = Digest.computeToQMD5(inputStream);
            String fileName;
            if (!TextUtils.isEmpty(md5Value)) {
                fileName = TextUtils.isEmpty(customFileName) ? new StringBuffer().append("IMG_").append(md5Value.toUpperCase()).append(suffix).toString() : customFileName;
            } else {
                fileName = TextUtils.isEmpty(customFileName) ? DateUtils.getCreateFileName("IMG_") + suffix : customFileName;
            }
            if (filesDir != null) {
                String newPath = new StringBuffer().append(filesDir).append(File.separator).append(fileName).toString();
                File outFile = new File(newPath);
                if (outFile.exists()) {
                    return newPath;
                }
                boolean copyFileSuccess = PictureFileUtils.copyFile(inputStream, outFile);
                if (copyFileSuccess) {
                    return newPath;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 解析Android Q版本下音频
     * #耗时操作需要放在子线程中操作
     *
     * @param ctx
     * @param path
     * @param customFileName
     * @param mineType
     * @return
     */
    public static String parseAudioPathToAndroidQ(Context ctx, String path, String customFileName, String mineType) {
        try {
            String suffix = PictureMimeType.getLastImgSuffix(mineType);
            String filesDir = PictureFileUtils.getAudioDiskCacheDir(ctx.getApplicationContext());
            ParcelFileDescriptor parcelFileDescriptor =
                    ctx.getContentResolver().openFileDescriptor(Uri.parse(path), "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            FileInputStream inputStream = new FileInputStream(fileDescriptor);
            String md5Value = Digest.computeToQMD5(inputStream);
            String fileName;
            if (!TextUtils.isEmpty(md5Value)) {
                fileName = TextUtils.isEmpty(customFileName) ? new StringBuffer().append("AUD_").append(md5Value.toUpperCase()).append(suffix).toString() : customFileName;
            } else {
                fileName = TextUtils.isEmpty(customFileName) ? DateUtils.getCreateFileName("AUD_") + suffix : customFileName;
            }
            if (filesDir != null) {
                String newPath = new StringBuffer().append(filesDir).append(File.separator).append(fileName).toString();
                File outFile = new File(newPath);
                if (outFile.exists()) {
                    return newPath;
                }
                boolean copyFileSuccess = PictureFileUtils.copyFile(inputStream, outFile);
                if (copyFileSuccess) {
                    return newPath;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
 
    /**
     * 复制一份至自己应用沙盒内
     *
     * @param context
     * @param cameraFileName
     * @param media
     * @return
     */
    @Nullable
    public static String getPathToAndroidQ(Context context, String cameraFileName, LocalMedia media) {
        if (PictureMimeType.eqVideo(media.getMimeType())) {
            return AndroidQTransformUtils.parseVideoPathToAndroidQ
                    (context.getApplicationContext(), media.getPath(), cameraFileName, media.getMimeType());
        } else if (PictureMimeType.eqAudio(media.getMimeType())) {
            return AndroidQTransformUtils.parseAudioPathToAndroidQ
                    (context.getApplicationContext(), media.getPath(), cameraFileName, media.getMimeType());
        } else {
            return AndroidQTransformUtils.parseImagePathToAndroidQ
                    (context.getApplicationContext(), media.getPath(), cameraFileName, media.getMimeType());
        }
    }
 
    /**
     * 复制文件至AndroidQ手机相册目录
     *
     * @param context
     * @param inputUri
     * @param outUri
     */
    public static void copyPathToDCIM(Context context, Uri inputUri, Uri outUri) {
        try {
            OutputStream outputStream = context.getContentResolver()
                    .openOutputStream(outUri);
            byte[] buffer = new byte[1024 * 8];
            int read;
            ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(inputUri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            FileInputStream inputStream = new FileInputStream(fileDescriptor);
            while ((read = inputStream.read(buffer)) > -1) {
                outputStream.write(buffer, 0, read);
            }
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}