|
8 | 8 | import android.content.ContentResolver;
|
9 | 9 | import android.database.Cursor;
|
10 | 10 | import android.net.Uri;
|
| 11 | +import android.os.ParcelFileDescriptor; |
11 | 12 | import android.provider.OpenableColumns;
|
12 | 13 | import android.util.Log;
|
13 | 14 |
|
14 | 15 | import java.io.File;
|
15 | 16 | import java.io.FileNotFoundException;
|
| 17 | +import java.io.IOException; |
16 | 18 | import java.io.InputStream;
|
17 | 19 | import java.io.UnsupportedEncodingException;
|
18 | 20 | import java.net.URLDecoder;
|
19 | 21 |
|
20 | 22 | public class UriInterpretation {
|
21 | 23 |
|
22 |
| - private long size = -1; |
23 |
| - private String name = null; |
| 24 | + private long size = -1; |
| 25 | + private String name = null; |
24 | 26 | private String path = null;
|
25 |
| - private String mime; |
26 |
| - private boolean isDirectory = false; |
27 |
| - private Uri uri; |
| 27 | + private String mime; |
| 28 | + private boolean isDirectory = false; |
| 29 | + private Uri uri; |
28 | 30 | private ContentResolver contentResolver;
|
29 | 31 |
|
30 |
| - public InputStream getInputStream() throws FileNotFoundException { |
31 |
| - return contentResolver.openInputStream(uri); |
32 |
| - } |
| 32 | + public InputStream getInputStream() throws FileNotFoundException { |
| 33 | + return contentResolver.openInputStream(uri); |
| 34 | + } |
33 | 35 |
|
34 |
| - public UriInterpretation(Uri uri, ContentResolver contentResolver) { |
35 |
| - this.uri = uri; |
| 36 | + public UriInterpretation(Uri uri, ContentResolver contentResolver) { |
| 37 | + this.uri = uri; |
| 38 | + |
| 39 | + this.contentResolver = contentResolver; |
| 40 | + |
| 41 | + Cursor metadataCursor = contentResolver.query(uri, new String[]{ |
| 42 | + OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null, |
| 43 | + null, null); |
| 44 | + |
| 45 | + if (metadataCursor != null) { |
| 46 | + try { |
| 47 | + if (metadataCursor.moveToFirst()) { |
| 48 | + path = name = metadataCursor.getString(0); |
| 49 | + size = metadataCursor.getInt(1); |
| 50 | + |
| 51 | + // sometimes this ContentResolver gives me the wrong file size ... |
| 52 | + // here is the fix |
| 53 | + // https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size |
| 54 | + try { |
| 55 | + ParcelFileDescriptor pfd = contentResolver.openFileDescriptor(uri, "r"); |
| 56 | + size = pfd.getStatSize(); |
| 57 | + pfd.close(); |
| 58 | + } catch (FileNotFoundException e) { |
| 59 | + // throw new RuntimeException(e); |
| 60 | + // since this is plan B, I don't care about the exception |
| 61 | + } catch (IOException e) { |
| 62 | + // throw new RuntimeException(e); |
| 63 | + // since this is plan B, I don't care about the exception |
| 64 | + } |
| 65 | + } |
| 66 | + } finally { |
| 67 | + metadataCursor.close(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (name == null) { |
| 72 | + name = uri.getLastPathSegment(); |
| 73 | + path = uri.toString(); |
| 74 | + } |
36 | 75 |
|
37 |
| - this.contentResolver = contentResolver; |
| 76 | + getMime(uri, contentResolver); |
38 | 77 |
|
39 |
| - Cursor metadataCursor = contentResolver.query(uri, new String[] { |
40 |
| - OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null, |
41 |
| - null, null); |
| 78 | + getFileSize(uri); |
42 | 79 |
|
43 |
| - if (metadataCursor != null) { |
44 |
| - try { |
45 |
| - if (metadataCursor.moveToFirst()) { |
46 |
| - path = name = metadataCursor.getString(0); |
47 |
| - size = metadataCursor.getInt(1); |
48 |
| - } |
49 |
| - } finally { |
50 |
| - metadataCursor.close(); |
51 |
| - } |
52 |
| - } |
| 80 | + } |
53 | 81 |
|
54 |
| - if (name == null) { |
55 |
| - name = uri.getLastPathSegment(); |
56 |
| - path = uri.toString(); |
57 |
| - } |
58 |
| - |
59 |
| - getMime(uri, contentResolver); |
60 |
| - |
61 |
| - getFileSize(uri); |
62 |
| - |
63 |
| - } |
64 |
| - |
65 |
| - private void getFileSize(Uri uri) { |
66 |
| - if (size <= 0) { |
67 |
| - String uriString = uri.toString(); |
68 |
| - if (uriString.startsWith("file://")) { |
69 |
| - File f = new File(uriString.substring("file://".length())); |
70 |
| - isDirectory = f.isDirectory(); |
71 |
| - if (isDirectory) { |
72 |
| - // Log.v(Util.myLogName, "We are dealing with a directory."); |
73 |
| - size = 0; |
74 |
| - return; |
75 |
| - } |
76 |
| - size = f.length(); |
77 |
| - if (size == 0) { |
78 |
| - try { |
79 |
| - uriString = URLDecoder.decode(uriString, "UTF-8").substring( |
80 |
| - "file://".length()); |
81 |
| - f = new File(uriString); |
82 |
| - size = f.length(); |
83 |
| - }catch (UnsupportedEncodingException e) { |
84 |
| - // |
85 |
| - } |
86 |
| - } |
87 |
| - ///Log.v(Util.myLogName, "zzz" + size); |
88 |
| - |
89 |
| - } else { |
90 |
| - try { |
91 |
| - File f = new File(uriString); |
92 |
| - isDirectory = f.isDirectory(); |
93 |
| - return; |
94 |
| - } catch (Exception e) { |
95 |
| - Log.v(Util.myLogName, "Not a file... " + uriString); |
96 |
| - e.printStackTrace(); |
97 |
| - } |
98 |
| - Log.v(Util.myLogName, "Not a file: " + uriString); |
99 |
| - |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - private void getMime(Uri uri, ContentResolver contentResolver) { |
105 |
| - mime = contentResolver.getType(uri); |
106 |
| - if (mime == null || name == null) { |
107 |
| - mime = "application/octet-stream"; |
108 |
| - if(name == null){ |
109 |
| - return; |
110 |
| - } |
111 |
| - } |
112 |
| - if (mime.equals("application/octet-stream")) { |
113 |
| - // we can do better than that |
114 |
| - int pos = name.lastIndexOf('.'); |
115 |
| - if (pos < 0) |
116 |
| - return; |
117 |
| - String extension = name.substring(pos).toLowerCase(); |
118 |
| - if (extension.equals(".jpg")) { |
119 |
| - mime = "image/jpeg"; |
120 |
| - return; |
121 |
| - } |
122 |
| - if (extension.equals(".png")) { |
123 |
| - mime = "image/png"; |
124 |
| - return; |
125 |
| - } |
126 |
| - if (extension.equals(".gif")) { |
127 |
| - mime = "image/gif"; |
128 |
| - return; |
129 |
| - } |
130 |
| - if (extension.equals(".mp4")) { |
131 |
| - mime = "video/mp4"; |
132 |
| - return; |
133 |
| - } |
134 |
| - if (extension.equals(".avi")) { |
135 |
| - mime = "video/avi"; |
136 |
| - return; |
137 |
| - } |
138 |
| - if (extension.equals(".mov")) { |
139 |
| - mime = "video/mov"; |
140 |
| - return; |
141 |
| - } |
142 |
| - if (extension.equals(".vcf")) { |
143 |
| - mime = "text/x-vcard"; |
144 |
| - return; |
145 |
| - } |
146 |
| - if (extension.equals(".txt")) { |
147 |
| - mime = "text/plain"; |
148 |
| - return; |
149 |
| - } |
150 |
| - if (extension.equals(".html")) { |
151 |
| - mime = "text/html"; |
152 |
| - return; |
153 |
| - } |
154 |
| - if (extension.equals(".json")) { |
155 |
| - mime = "application/json"; |
156 |
| - return; |
157 |
| - } |
158 |
| - if (extension.equals(".epub")) { |
159 |
| - mime = "application/epub+zip"; |
160 |
| - return; |
161 |
| - } |
162 |
| - |
163 |
| - } |
164 |
| - |
165 |
| - } |
| 82 | + private void getFileSize(Uri uri) { |
| 83 | + if (size <= 0) { |
| 84 | + String uriString = uri.toString(); |
| 85 | + if (uriString.startsWith("file://")) { |
| 86 | + File f = new File(uriString.substring("file://".length())); |
| 87 | + isDirectory = f.isDirectory(); |
| 88 | + if (isDirectory) { |
| 89 | + // Log.v(Util.myLogName, "We are dealing with a directory."); |
| 90 | + size = 0; |
| 91 | + return; |
| 92 | + } |
| 93 | + size = f.length(); |
| 94 | + if (size == 0) { |
| 95 | + try { |
| 96 | + uriString = URLDecoder.decode(uriString, "UTF-8").substring( |
| 97 | + "file://".length()); |
| 98 | + f = new File(uriString); |
| 99 | + size = f.length(); |
| 100 | + } catch (UnsupportedEncodingException e) { |
| 101 | + // |
| 102 | + } |
| 103 | + } |
| 104 | + ///Log.v(Util.myLogName, "zzz" + size); |
| 105 | + |
| 106 | + } else { |
| 107 | + try { |
| 108 | + File f = new File(uriString); |
| 109 | + isDirectory = f.isDirectory(); |
| 110 | + return; |
| 111 | + } catch (Exception e) { |
| 112 | + Log.v(Util.myLogName, "Not a file... " + uriString); |
| 113 | + e.printStackTrace(); |
| 114 | + } |
| 115 | + Log.v(Util.myLogName, "Not a file: " + uriString); |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void getMime(Uri uri, ContentResolver contentResolver) { |
| 122 | + mime = contentResolver.getType(uri); |
| 123 | + if (mime == null || name == null) { |
| 124 | + mime = "application/octet-stream"; |
| 125 | + if (name == null) { |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + if (mime.equals("application/octet-stream")) { |
| 130 | + // we can do better than that |
| 131 | + int pos = name.lastIndexOf('.'); |
| 132 | + if (pos < 0) |
| 133 | + return; |
| 134 | + String extension = name.substring(pos).toLowerCase(); |
| 135 | + if (extension.equals(".jpg")) { |
| 136 | + mime = "image/jpeg"; |
| 137 | + return; |
| 138 | + } |
| 139 | + if (extension.equals(".png")) { |
| 140 | + mime = "image/png"; |
| 141 | + return; |
| 142 | + } |
| 143 | + if (extension.equals(".gif")) { |
| 144 | + mime = "image/gif"; |
| 145 | + return; |
| 146 | + } |
| 147 | + if (extension.equals(".mp4")) { |
| 148 | + mime = "video/mp4"; |
| 149 | + return; |
| 150 | + } |
| 151 | + if (extension.equals(".avi")) { |
| 152 | + mime = "video/avi"; |
| 153 | + return; |
| 154 | + } |
| 155 | + if (extension.equals(".mov")) { |
| 156 | + mime = "video/mov"; |
| 157 | + return; |
| 158 | + } |
| 159 | + if (extension.equals(".vcf")) { |
| 160 | + mime = "text/x-vcard"; |
| 161 | + return; |
| 162 | + } |
| 163 | + if (extension.equals(".txt")) { |
| 164 | + mime = "text/plain"; |
| 165 | + return; |
| 166 | + } |
| 167 | + if (extension.equals(".html")) { |
| 168 | + mime = "text/html"; |
| 169 | + return; |
| 170 | + } |
| 171 | + if (extension.equals(".json")) { |
| 172 | + mime = "application/json"; |
| 173 | + return; |
| 174 | + } |
| 175 | + if (extension.equals(".epub")) { |
| 176 | + mime = "application/epub+zip"; |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + } |
166 | 183 |
|
167 | 184 | public long getSize() {
|
168 | 185 | return size;
|
|
0 commit comments