From 7402f4bf9d8dd6df0e0bfe1a2e33d5c27d8106e9 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 5 Mar 2019 12:48:22 +0000 Subject: [PATCH] Closes Android input streams and delivers its exceptions to its caller --- .../plugin/filepicker/FilePickerPlugin.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java b/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java index 2c300e0..8b2af83 100644 --- a/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java +++ b/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java @@ -15,6 +15,7 @@ import android.webkit.MimeTypeMap; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; @@ -62,14 +63,14 @@ public class FilePickerPlugin implements MethodCallHandler { try { fos = new FileOutputStream(cloudFile); - try{ + try { BufferedOutputStream out = new BufferedOutputStream(fos); InputStream in = instance.activeContext().getContentResolver().openInputStream(uri); byte[] buffer = new byte[8192]; int len = 0; - while ((len = in.read(buffer)) >= 0){ + while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); } @@ -77,15 +78,18 @@ public class FilePickerPlugin implements MethodCallHandler { } finally { fos.getFD().sync(); } - } catch (Exception e) { - e.printStackTrace(); + try { + fos.close(); + } catch(IOException ex) { + result.error(TAG, "Failed to close file streams: " + e.getMessage(),null); + } + result.error(TAG, "Failed to retrieve path: " + e.getMessage(),null); } Log.i(TAG, "Cloud file loaded and cached on:" + cloudFile); fullPath = cloudFile; } - Log.i(TAG, "Absolute file path:" + fullPath); result.success(fullPath); } @@ -147,8 +151,6 @@ public class FilePickerPlugin implements MethodCallHandler { } switch (type) { - case "PDF": - return "application/pdf"; case "VIDEO": return "video/*"; case "ANY":