@@ -267,6 +267,8 @@ private WritableMap getMetadata(Uri uri) {
267267 if (!cursor .isNull (displayNameIndex )) {
268268 String fileName = cursor .getString (displayNameIndex );
269269 map .putString (FIELD_NAME , fileName );
270+ } else {
271+ map .putNull (FIELD_NAME );
270272 }
271273 int mimeIndex = cursor .getColumnIndex (DocumentsContract .Document .COLUMN_MIME_TYPE );
272274 if (!cursor .isNull (mimeIndex )) {
@@ -275,6 +277,8 @@ private WritableMap getMetadata(Uri uri) {
275277 int sizeIndex = cursor .getColumnIndex (OpenableColumns .SIZE );
276278 if (!cursor .isNull (sizeIndex )) {
277279 map .putInt (FIELD_SIZE , cursor .getInt (sizeIndex ));
280+ } else {
281+ map .putNull (FIELD_SIZE );
278282 }
279283 }
280284 }
@@ -284,63 +288,48 @@ private WritableMap getMetadata(Uri uri) {
284288 }
285289
286290 private void prepareFileUri (Context context , WritableMap map , Uri uri ) {
287- if (copyTo != null ) {
288- File dir = context .getCacheDir ();
289- if (copyTo .equals ("documentDirectory" )) {
290- dir = context .getFilesDir ();
291- }
292- // we don't want to rename the file so we put it into a unique location
293- dir = new File (dir , UUID .randomUUID ().toString ());
294- try {
295- boolean didCreateDir = dir .mkdir ();
296- if (!didCreateDir ) {
297- throw new IOException ("failed to create directory at " + dir .getAbsolutePath ());
298- }
299- String fileName = map .getString (FIELD_NAME );
300- if (fileName == null ) {
301- fileName = String .valueOf (System .currentTimeMillis ());
302- }
303- File destFile = new File (dir , fileName );
304- String copyPath = copyFile (context , uri , destFile );
305- map .putString (FIELD_FILE_COPY_URI , copyPath );
306- } catch (Exception e ) {
307- e .printStackTrace ();
308- map .putNull (FIELD_FILE_COPY_URI );
309- map .putString (FIELD_COPY_ERROR , e .getLocalizedMessage ());
310- }
311- } else {
291+ if (copyTo == null ) {
312292 map .putNull (FIELD_FILE_COPY_URI );
293+ } else {
294+ copyFileToLocalStorage (context , map , uri );
313295 }
314296 }
315297
316- public static String copyFile (Context context , Uri uri , File destFile ) throws IOException {
317- InputStream in = null ;
318- FileOutputStream out = null ;
298+ private void copyFileToLocalStorage (Context context , WritableMap map , Uri uri ) {
299+ File dir = context .getCacheDir ();
300+ if (copyTo .equals ("documentDirectory" )) {
301+ dir = context .getFilesDir ();
302+ }
303+ // we don't want to rename the file so we put it into a unique location
304+ dir = new File (dir , UUID .randomUUID ().toString ());
319305 try {
320- in = context .getContentResolver ().openInputStream (uri );
321- if (in != null ) {
322- out = new FileOutputStream (destFile );
323- byte [] buffer = new byte [1024 ];
324- int len ;
325- while ((len = in .read (buffer )) > 0 ) {
326- out .write (buffer , 0 , len );
327- }
328- out .close ();
329- in .close ();
330- return destFile .toURI ().toString ();
331- } else {
332- throw new NullPointerException ("Invalid input stream" );
306+ boolean didCreateDir = dir .mkdir ();
307+ if (!didCreateDir ) {
308+ throw new IOException ("failed to create directory at " + dir .getAbsolutePath ());
333309 }
310+ String fileName = map .getString (FIELD_NAME );
311+ if (fileName == null ) {
312+ fileName = String .valueOf (System .currentTimeMillis ());
313+ }
314+ File destFile = new File (dir , fileName );
315+ Uri copyPath = copyFile (context , uri , destFile );
316+ map .putString (FIELD_FILE_COPY_URI , copyPath .toString ());
334317 } catch (Exception e ) {
335- try {
336- if (in != null ) {
337- in .close ();
338- }
339- if (out != null ) {
340- out .close ();
341- }
342- } catch (IOException ignored ) {}
343- throw e ;
318+ e .printStackTrace ();
319+ map .putNull (FIELD_FILE_COPY_URI );
320+ map .putString (FIELD_COPY_ERROR , e .getLocalizedMessage ());
321+ }
322+ }
323+
324+ public static Uri copyFile (Context context , Uri uri , File destFile ) throws IOException {
325+ try (InputStream inputStream = context .getContentResolver ().openInputStream (uri );
326+ FileOutputStream outputStream = new FileOutputStream (destFile )) {
327+ byte [] buf = new byte [8192 ];
328+ int len ;
329+ while ((len = inputStream .read (buf )) > 0 ) {
330+ outputStream .write (buf , 0 , len );
331+ }
332+ return Uri .fromFile (destFile );
344333 }
345334 }
346335 }
0 commit comments