25
25
import com .facebook .react .bridge .WritableArray ;
26
26
import com .facebook .react .bridge .WritableMap ;
27
27
28
+ import java .io .BufferedReader ;
29
+ import java .io .File ;
30
+ import java .io .FileInputStream ;
31
+ import java .io .FileNotFoundException ;
32
+ import java .io .FileOutputStream ;
33
+ import java .io .IOException ;
34
+ import java .io .InputStream ;
35
+ import java .io .InputStreamReader ;
36
+ import java .io .OutputStream ;
37
+ import java .util .logging .Logger ;
38
+
28
39
/**
29
40
* @see <a href="https://developer.android.com/guide/topics/providers/document-provider.html">android documentation</a>
30
41
*/
@@ -174,24 +185,49 @@ public void onShowActivityResult(int resultCode, Intent data, Promise promise) {
174
185
}
175
186
}
176
187
177
- private WritableMap getMetadata (Uri uri ) {
188
+ private WritableMap getMetadata (Uri uri ) throws FileNotFoundException {
178
189
WritableMap map = Arguments .createMap ();
179
190
180
- map .putString (FIELD_URI , uri .toString ());
181
191
182
192
ContentResolver contentResolver = getReactApplicationContext ().getContentResolver ();
183
193
184
194
map .putString (FIELD_TYPE , contentResolver .getType (uri ));
185
195
186
196
Cursor cursor = contentResolver .query (uri , null , null , null , null , null );
187
-
188
197
try {
189
198
if (cursor != null && cursor .moveToFirst ()) {
199
+ String fileName = "" ;
190
200
int displayNameIndex = cursor .getColumnIndex (OpenableColumns .DISPLAY_NAME );
191
201
if (!cursor .isNull (displayNameIndex )) {
192
- map .putString (FIELD_NAME , cursor .getString (displayNameIndex ));
202
+ fileName = cursor .getString (displayNameIndex );
203
+ map .putString (FIELD_NAME , fileName );
204
+ }
205
+
206
+ if (uri != null && "content" .equals (uri .getScheme ())) {
207
+ int flag = cursor .getInt (0 );
208
+ if (DocumentPickerModule .isVirtualFile (flag )) {
209
+ try {
210
+ InputStream input = DocumentPickerModule .getInputStreamForVirtualFile (contentResolver ,uri ,contentResolver .getType (uri ));
211
+ File file = new File (getReactApplicationContext ().getCacheDir (),fileName );
212
+ OutputStream output = new FileOutputStream (file );
213
+ byte [] buffer = new byte [4 * 1024 ]; // or other buffer size
214
+ int read ;
215
+ while ((read = input .read (buffer )) != -1 ) {
216
+ output .write (buffer , 0 , read );
217
+ }
218
+ output .flush ();
219
+ map .putString (FIELD_URI , file .getPath ());
220
+ } catch (IOException e ) {
221
+ throw new FileNotFoundException ();
222
+ }
223
+ } else {
224
+ map .putString (FIELD_URI , uri .toString ());
225
+ }
226
+ } else {
227
+ map .putString (FIELD_URI , uri .toString ());
193
228
}
194
229
230
+
195
231
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
196
232
int mimeIndex = cursor .getColumnIndex (DocumentsContract .Document .COLUMN_MIME_TYPE );
197
233
if (!cursor .isNull (mimeIndex )) {
@@ -212,4 +248,28 @@ private WritableMap getMetadata(Uri uri) {
212
248
213
249
return map ;
214
250
}
251
+
252
+ private static boolean isVirtualFile (int flags ) {
253
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
254
+ return (flags & DocumentsContract .Document .FLAG_VIRTUAL_DOCUMENT ) != 0 ;
255
+ } else {
256
+ return false ;
257
+ }
258
+ }
259
+
260
+ private static InputStream getInputStreamForVirtualFile (ContentResolver resolver , Uri uri , String mimeTypeFilter )
261
+ throws IOException {
262
+
263
+ String [] openableMimeTypes = resolver .getStreamTypes (uri , mimeTypeFilter );
264
+
265
+ if (openableMimeTypes == null ||
266
+ openableMimeTypes .length < 1 ) {
267
+ throw new FileNotFoundException ();
268
+ }
269
+
270
+ return resolver
271
+ .openTypedAssetFileDescriptor (uri , openableMimeTypes [0 ], null )
272
+ .createInputStream ();
273
+ }
274
+
215
275
}
0 commit comments