|
1 | 1 | package com.blankj.utilcode.util;
|
2 | 2 |
|
3 | 3 | import android.content.ContentResolver;
|
| 4 | +import android.content.ContentUris; |
4 | 5 | import android.content.CursorLoader;
|
5 | 6 | import android.database.Cursor;
|
6 | 7 | import android.net.Uri;
|
7 | 8 | import android.os.Build;
|
| 9 | +import android.os.Environment; |
| 10 | +import android.os.ParcelFileDescriptor; |
| 11 | +import android.provider.DocumentsContract; |
8 | 12 | import android.provider.MediaStore;
|
9 | 13 | import android.support.annotation.NonNull;
|
| 14 | +import android.support.annotation.Nullable; |
10 | 15 | import android.support.v4.content.FileProvider;
|
| 16 | +import android.util.Log; |
11 | 17 |
|
12 | 18 | import java.io.File;
|
| 19 | +import java.io.FileDescriptor; |
13 | 20 |
|
14 | 21 | /**
|
15 | 22 | * <pre>
|
@@ -43,24 +50,85 @@ public static Uri file2Uri(@NonNull final File file) {
|
43 | 50 | /**
|
44 | 51 | * Uri to file.
|
45 | 52 | *
|
46 |
| - * @param uri The uri. |
47 |
| - * @param columnName The name of the target column. |
48 |
| - * <p>e.g. {@link MediaStore.Images.Media#DATA}</p> |
| 53 | + * @param uri The uri. |
49 | 54 | * @return file
|
50 | 55 | */
|
51 |
| - public static File uri2File(@NonNull final Uri uri, final String columnName) { |
52 |
| - if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { |
53 |
| - return new File(uri.getPath()); |
| 56 | + public static File uri2File(@NonNull final Uri uri) { |
| 57 | + Log.d("UriUtils", uri.toString()); |
| 58 | + String authority = uri.getAuthority(); |
| 59 | + String scheme = uri.getScheme(); |
| 60 | + if (ContentResolver.SCHEME_FILE.equals(scheme)) { |
| 61 | + String path = uri.getPath(); |
| 62 | + if (path != null) return new File(path); |
| 63 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 0"); |
| 64 | + return null; |
| 65 | + } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { |
| 66 | + return getFileFromUri(uri, null, null); |
| 67 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT |
| 68 | + && DocumentsContract.isDocumentUri(Utils.getApp(), uri)) { |
| 69 | + if ("com.android.externalstorage.documents".equals(authority)) { |
| 70 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 71 | + final String[] split = docId.split(":"); |
| 72 | + final String type = split[0]; |
| 73 | + if ("primary".equalsIgnoreCase(type)) { |
| 74 | + return new File(Environment.getExternalStorageDirectory() + "/" + split[1]); |
| 75 | + } |
| 76 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 2"); |
| 77 | + return null; |
| 78 | + } else if ("com.android.providers.downloads.documents".equals(authority)) { |
| 79 | + final String id = DocumentsContract.getDocumentId(uri); |
| 80 | + final Uri contentUri = ContentUris.withAppendedId( |
| 81 | + Uri.parse("content://downloads/public_downloads"), |
| 82 | + Long.valueOf(id) |
| 83 | + ); |
| 84 | + return getFileFromUri(contentUri, null, null); |
| 85 | + } else if ("com.android.providers.media.documents".equals(authority)) { |
| 86 | + final String docId = DocumentsContract.getDocumentId(uri); |
| 87 | + final String[] split = docId.split(":"); |
| 88 | + final String type = split[0]; |
| 89 | + Uri contentUri; |
| 90 | + if ("image".equals(type)) { |
| 91 | + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 92 | + } else if ("video".equals(type)) { |
| 93 | + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 94 | + } else if ("audio".equals(type)) { |
| 95 | + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 96 | + } else { |
| 97 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 3"); |
| 98 | + return null; |
| 99 | + } |
| 100 | + final String selection = "_id=?"; |
| 101 | + final String[] selectionArgs = new String[]{split[1]}; |
| 102 | + return getFileFromUri(contentUri, selection, selectionArgs); |
| 103 | + } else { |
| 104 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 4"); |
| 105 | + return null; |
| 106 | + } |
| 107 | + } else { |
| 108 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 5"); |
| 109 | + return null; |
54 | 110 | }
|
| 111 | + } |
| 112 | + |
| 113 | + private static File getFileFromUri(@NonNull final Uri uri) { |
| 114 | + return getFileFromUri(uri, null, null); |
| 115 | + } |
| 116 | + |
| 117 | + private static File getFileFromUri(@NonNull final Uri uri, |
| 118 | + final String selection, |
| 119 | + final String[] selectionArgs) { |
55 | 120 | CursorLoader cl = new CursorLoader(Utils.getApp());
|
56 | 121 | cl.setUri(uri);
|
57 |
| - cl.setProjection(new String[]{columnName}); |
| 122 | + cl.setProjection(new String[]{"_data"}); |
58 | 123 | Cursor cursor = null;
|
59 | 124 | try {
|
60 | 125 | cursor = cl.loadInBackground();
|
61 |
| - int columnIndex = cursor.getColumnIndexOrThrow(columnName); |
| 126 | + int columnIndex = cursor.getColumnIndexOrThrow("_data"); |
62 | 127 | cursor.moveToFirst();
|
63 | 128 | return new File(cursor.getString(columnIndex));
|
| 129 | + } catch (Exception e) { |
| 130 | + Log.d("UriUtils", uri.toString() + " parse failed. -> 1"); |
| 131 | + return null; |
64 | 132 | } finally {
|
65 | 133 | if (cursor != null) {
|
66 | 134 | cursor.close();
|
|
0 commit comments