@@ -261,6 +261,116 @@ private File downloadFile(String url, String fileName, Boolean cache, String fil
261261
262262
263263
264+ } catch (FileNotFoundException e ) {
265+ e .printStackTrace ();
266+ callback .invoke (ERROR_FILE_NOT_FOUND );
267+ return null ;
268+ } catch (IOException e ) {
269+ e .printStackTrace ();
270+ callback .invoke (ERROR_UNKNOWN_ERROR );
271+ return null ;
272+ }
273+ }
274+ private File copyFile (InputStream in , String fileName , Boolean cache , String fileType , byte [] bytesData , Callback callback ) {
275+
276+ try {
277+ Context context = getReactApplicationContext ().getBaseContext ();
278+ File outputDir = context .getCacheDir ();
279+ if (bytesData .length > 0 ) {
280+ // use cache
281+ File f = cache != null && cache ? new File (outputDir , fileName ) : File .createTempFile (FILE_TYPE_PREFIX , "." + fileType ,
282+ outputDir );
283+ System .out .println ("Bytes will be creating a file" );
284+ final FileOutputStream fileOutputStream ;
285+ try {
286+ fileOutputStream = new FileOutputStream (f );
287+ } catch (FileNotFoundException e ) {
288+ e .printStackTrace ();
289+ return null ;
290+ }
291+
292+ final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (
293+ fileOutputStream );
294+ try {
295+ bufferedOutputStream .write (bytesData );
296+ } catch (IOException e ) {
297+ e .printStackTrace ();
298+ return null ;
299+ } finally {
300+ try {
301+ bufferedOutputStream .close ();
302+ } catch (IOException e ) {
303+ e .printStackTrace ();
304+ }
305+ }
306+ return f ;
307+ } else {
308+ String extension = MimeTypeMap .getFileExtensionFromUrl (fileName );
309+ System .out .println ("Extensions DownloadFile " + extension );
310+ if (extension .equals ("" ) && fileType .equals ("" )) {
311+ extension = "pdf" ;
312+ System .out .println ("extension (default): " + extension );
313+ }
314+
315+ if (fileType != "" && extension .equals ("" )) {
316+ extension = fileType ;
317+ System .out .println ("extension (default): " + extension );
318+ }
319+
320+ // check has extension
321+ if (fileName .indexOf ("\\ ." ) == -1 ) {
322+ fileName = fileName + '.' + extension ;
323+ }
324+ // if use cache, check exist
325+ if (cache != null && cache ) {
326+ File existFile = new File (outputDir , fileName );
327+ if (existFile .exists ()) {
328+ return existFile ;
329+ }
330+ }
331+
332+
333+ File f ;
334+ try {
335+ // use cache
336+ f = cache != null && cache ? new File (outputDir , fileName )
337+ : File .createTempFile (FILE_TYPE_PREFIX , "." + extension , outputDir );
338+
339+ // make sure the receiving app can read this file
340+ f .setReadable (true , false );
341+ System .out .println (f .getPath ());
342+
343+ try {
344+ FileOutputStream outStream = new FileOutputStream (f );
345+ try {
346+ // Transfer bytes from in to out
347+ byte [] buf = new byte [1024 ];
348+ int len ;
349+ while ((len = in .read (buf )) > 0 ) {
350+ outStream .write (buf , 0 , len );
351+ }
352+ } finally {
353+ outStream .close ();
354+ }
355+ } finally {
356+ in .close ();
357+ }
358+
359+ if (f .exists ()) {
360+ System .out .println ("File exists" );
361+ } else {
362+ System .out .println ("File doesn't exist" );
363+ }
364+
365+ return f ;
366+ } catch (Exception err ) {
367+ err .printStackTrace ();
368+ }
369+
370+ return null ;
371+ }
372+
373+
264374 } catch (FileNotFoundException e ) {
265375 e .printStackTrace ();
266376 callback .invoke (ERROR_FILE_NOT_FOUND );
@@ -281,6 +391,7 @@ private File downloadFile(String url, String fileName, Boolean cache, String fil
281391 private static String getMimeType (String url ) {
282392 String mimeType = null ;
283393 System .out .println ("Url: " + url );
394+ url = url .replaceAll (" " , "" );
284395 String extension = MimeTypeMap .getFileExtensionFromUrl (url );
285396 if (extension != null ) {
286397 mimeType = MimeTypeMap .getSingleton ().getMimeTypeFromExtension (extension );
@@ -315,12 +426,20 @@ public FileDownloaderAsyncTask(Callback callback,
315426
316427 @ Override
317428 protected File doInBackground (Void ... arg0 ) {
318- if (!url .startsWith ("file://" )) {
319- System .out .println ("Url to download" +url );
429+ if (url .startsWith ("content://" )) {
430+ File file = null ;
431+ try {
432+ InputStream in = getCurrentActivity ().getContentResolver ().openInputStream (Uri .parse (url ));
433+ file = copyFile (in , fileName , cache , fileType , bytesData , callback );
434+ } catch (FileNotFoundException e ) {
435+ System .out .println (e );
436+ }
437+ return file ;
438+ } else if (!url .startsWith ("file://" )) {
439+ System .out .println ("Url to download" + url );
320440 return downloadFile (url , fileName , cache , fileType , bytesData , callback );
321441 } else {
322- File file = new File (url .replace ("file://" , "" ));
323- return file ;
442+ return new File (url .replace ("file://" , "" ));
324443 }
325444 }
326445
0 commit comments