-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
UriUtils.java
367 lines (341 loc) · 14.7 KB
/
UriUtils.java
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.blankj.utilcode.util;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import androidx.core.content.FileProvider;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/20
* desc : utils about uri
* </pre>
*/
public final class UriUtils {
private UriUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Resource to uri.
* <p>res2Uri([res type]/[res name]) -> res2Uri(drawable/icon), res2Uri(raw/icon)</p>
* <p>res2Uri([resource_id]) -> res2Uri(R.drawable.icon)</p>
*
* @param resPath The path of res.
* @return uri
*/
public static Uri res2Uri(String resPath) {
return Uri.parse("android.resource://" + Utils.getApp().getPackageName() + "/" + resPath);
}
/**
* File to uri.
*
* @param file The file.
* @return uri
*/
public static Uri file2Uri(final File file) {
if (!UtilsBridge.isFileExists(file)) return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String authority = Utils.getApp().getPackageName() + ".utilcode.fileprovider";
return FileProvider.getUriForFile(Utils.getApp(), authority, file);
} else {
return Uri.fromFile(file);
}
}
/**
* Uri to file.
*
* @param uri The uri.
* @return file
*/
public static File uri2File(final Uri uri) {
if (uri == null) return null;
File file = uri2FileReal(uri);
if (file != null) return file;
return copyUri2Cache(uri);
}
/**
* Uri to file, without creating the cache copy if the path cannot be resolved.
*
* @param uri The uri.
* @return file
*/
public static File uri2FileNoCacheCopy(final Uri uri) {
if (uri == null) return null;
return uri2FileReal(uri);
}
/**
* Uri to file.
*
* @param uri The uri.
* @return file
*/
private static File uri2FileReal(final Uri uri) {
Log.d("UriUtils", uri.toString());
String authority = uri.getAuthority();
String scheme = uri.getScheme();
String path = uri.getPath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && path != null) {
String[] externals = new String[]{"/external/", "/external_path/"};
File file = null;
for (String external : externals) {
if (path.startsWith(external)) {
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ path.replace(external, "/"));
if (file.exists()) {
Log.d("UriUtils", uri.toString() + " -> " + external);
return file;
}
}
}
file = null;
if (path.startsWith("/files_path/")) {
file = new File(Utils.getApp().getFilesDir().getAbsolutePath()
+ path.replace("/files_path/", "/"));
} else if (path.startsWith("/cache_path/")) {
file = new File(Utils.getApp().getCacheDir().getAbsolutePath()
+ path.replace("/cache_path/", "/"));
} else if (path.startsWith("/external_files_path/")) {
file = new File(Utils.getApp().getExternalFilesDir(null).getAbsolutePath()
+ path.replace("/external_files_path/", "/"));
} else if (path.startsWith("/external_cache_path/")) {
file = new File(Utils.getApp().getExternalCacheDir().getAbsolutePath()
+ path.replace("/external_cache_path/", "/"));
}
if (file != null && file.exists()) {
Log.d("UriUtils", uri.toString() + " -> " + path);
return file;
}
}
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
if (path != null) return new File(path);
Log.d("UriUtils", uri.toString() + " parse failed. -> 0");
return null;
}// end 0
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& DocumentsContract.isDocumentUri(Utils.getApp(), uri)) {
if ("com.android.externalstorage.documents".equals(authority)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return new File(Environment.getExternalStorageDirectory() + "/" + split[1]);
} else {
// Below logic is how External Storage provider build URI for documents
// http://stackoverflow.com/questions/28605278/android-5-sd-card-label
StorageManager mStorageManager = (StorageManager) Utils.getApp().getSystemService(Context.STORAGE_SERVICE);
try {
Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getUuid = storageVolumeClazz.getMethod("getUuid");
Method getState = storageVolumeClazz.getMethod("getState");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
Method isEmulated = storageVolumeClazz.getMethod("isEmulated");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
//String uuid = (String) getUuid.invoke(storageVolumeElement);
final boolean mounted = Environment.MEDIA_MOUNTED.equals(getState.invoke(storageVolumeElement))
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(getState.invoke(storageVolumeElement));
//if the media is not mounted, we need not get the volume details
if (!mounted) continue;
//Primary storage is already handled.
if ((Boolean) isPrimary.invoke(storageVolumeElement)
&& (Boolean) isEmulated.invoke(storageVolumeElement)) {
continue;
}
String uuid = (String) getUuid.invoke(storageVolumeElement);
if (uuid != null && uuid.equals(type)) {
return new File(getPath.invoke(storageVolumeElement) + "/" + split[1]);
}
}
} catch (Exception ex) {
Log.d("UriUtils", uri.toString() + " parse failed. " + ex.toString() + " -> 1_0");
}
}
Log.d("UriUtils", uri.toString() + " parse failed. -> 1_0");
return null;
}// end 1_0
else if ("com.android.providers.downloads.documents".equals(authority)) {
String id = DocumentsContract.getDocumentId(uri);
if (TextUtils.isEmpty(id)) {
Log.d("UriUtils", uri.toString() + " parse failed(id is null). -> 1_1");
return null;
}
if (id.startsWith("raw:")) {
return new File(id.substring(4));
} else if (id.startsWith("msf:")) {
id = id.split(":")[1];
}
long availableId = 0;
try {
availableId = Long.parseLong(id);
} catch (Exception e) {
return null;
}
String[] contentUriPrefixesToTry = new String[]{
"content://downloads/public_downloads",
"content://downloads/all_downloads",
"content://downloads/my_downloads"
};
for (String contentUriPrefix : contentUriPrefixesToTry) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), availableId);
try {
File file = getFileFromUri(contentUri, "1_1");
if (file != null) {
return file;
}
} catch (Exception ignore) {
}
}
Log.d("UriUtils", uri.toString() + " parse failed. -> 1_1");
return null;
}// end 1_1
else if ("com.android.providers.media.documents".equals(authority)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else {
Log.d("UriUtils", uri.toString() + " parse failed. -> 1_2");
return null;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getFileFromUri(contentUri, selection, selectionArgs, "1_2");
}// end 1_2
else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
return getFileFromUri(uri, "1_3");
}// end 1_3
else {
Log.d("UriUtils", uri.toString() + " parse failed. -> 1_4");
return null;
}// end 1_4
}// end 1
else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
return getFileFromUri(uri, "2");
}// end 2
else {
Log.d("UriUtils", uri.toString() + " parse failed. -> 3");
return null;
}// end 3
}
private static File getFileFromUri(final Uri uri, final String code) {
return getFileFromUri(uri, null, null, code);
}
private static File getFileFromUri(final Uri uri,
final String selection,
final String[] selectionArgs,
final String code) {
if ("com.google.android.apps.photos.content".equals(uri.getAuthority())) {
if (!TextUtils.isEmpty(uri.getLastPathSegment())) {
return new File(uri.getLastPathSegment());
}
} else if ("com.tencent.mtt.fileprovider".equals(uri.getAuthority())) {
String path = uri.getPath();
if (!TextUtils.isEmpty(path)) {
File fileDir = Environment.getExternalStorageDirectory();
return new File(fileDir, path.substring("/QQBrowser".length(), path.length()));
}
} else if ("com.huawei.hidisk.fileprovider".equals(uri.getAuthority())) {
String path = uri.getPath();
if (!TextUtils.isEmpty(path)) {
return new File(path.replace("/root", ""));
}
}
final Cursor cursor = Utils.getApp().getContentResolver().query(
uri, new String[]{"_data"}, selection, selectionArgs, null);
if (cursor == null) {
Log.d("UriUtils", uri.toString() + " parse failed(cursor is null). -> " + code);
return null;
}
try {
if (cursor.moveToFirst()) {
final int columnIndex = cursor.getColumnIndex("_data");
if (columnIndex > -1) {
return new File(cursor.getString(columnIndex));
} else {
Log.d("UriUtils", uri.toString() + " parse failed(columnIndex: " + columnIndex + " is wrong). -> " + code);
return null;
}
} else {
Log.d("UriUtils", uri.toString() + " parse failed(moveToFirst return false). -> " + code);
return null;
}
} catch (Exception e) {
Log.d("UriUtils", uri.toString() + " parse failed. -> " + code);
return null;
} finally {
cursor.close();
}
}
private static File copyUri2Cache(Uri uri) {
Log.d("UriUtils", "copyUri2Cache() called");
InputStream is = null;
try {
is = Utils.getApp().getContentResolver().openInputStream(uri);
File file = new File(Utils.getApp().getCacheDir(), "" + System.currentTimeMillis());
UtilsBridge.writeFileFromIS(file.getAbsolutePath(), is);
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* uri to input stream.
*
* @param uri The uri.
* @return the input stream
*/
public static byte[] uri2Bytes(Uri uri) {
if (uri == null) return null;
InputStream is = null;
try {
is = Utils.getApp().getContentResolver().openInputStream(uri);
return UtilsBridge.inputStream2Bytes(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("UriUtils", "uri to bytes failed.");
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}