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,22 +185,41 @@ 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
+ try {
208
+ InputStream input = DocumentPickerModule .getInputStreamForVirtualFile (contentResolver ,uri ,contentResolver .getType (uri ));
209
+ File file = new File (getReactApplicationContext ().getCacheDir (),fileName );
210
+ OutputStream output = new FileOutputStream (file );
211
+ byte [] buffer = new byte [4 * 1024 ]; // or other buffer size
212
+ int read ;
213
+ while ((read = input .read (buffer )) != -1 ) {
214
+ output .write (buffer , 0 , read );
215
+ }
216
+ output .flush ();
217
+ map .putString (FIELD_URI , file .getPath ());
218
+ } catch (IOException e ) {
219
+ throw new FileNotFoundException ();
220
+ }
221
+ } else {
222
+ map .putString (FIELD_URI , uri .toString ());
193
223
}
194
224
195
225
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
@@ -212,4 +242,20 @@ private WritableMap getMetadata(Uri uri) {
212
242
213
243
return map ;
214
244
}
245
+
246
+ private static InputStream getInputStreamForVirtualFile (ContentResolver resolver , Uri uri , String mimeTypeFilter )
247
+ throws IOException {
248
+
249
+ String [] openableMimeTypes = resolver .getStreamTypes (uri , mimeTypeFilter );
250
+
251
+ if (openableMimeTypes == null ||
252
+ openableMimeTypes .length < 1 ) {
253
+ throw new FileNotFoundException ();
254
+ }
255
+
256
+ return resolver
257
+ .openTypedAssetFileDescriptor (uri , openableMimeTypes [0 ], null )
258
+ .createInputStream ();
259
+ }
260
+
215
261
}
0 commit comments