From 9f0a811028a0944e9e23dad6002108a7ff17a6ec Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Tue, 22 Jun 2021 16:21:05 +0100 Subject: [PATCH] Fixes #616 and #746 --- CHANGELOG.md | 10 ++++ android/build.gradle | 2 +- .../flutter/plugin/filepicker/FileUtils.java | 47 +++++++++---------- example/lib/generated_plugin_registrant.dart | 2 +- lib/{src => _internal}/file_picker_web.dart | 4 +- pubspec.yaml | 4 +- 6 files changed, 38 insertions(+), 31 deletions(-) rename lib/{src => _internal}/file_picker_web.dart (98%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 035aefe..8996949 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 3.0.3 + +#### Web +- Removes analysis_options.yaml from the plugin and fixes the _Don't import implementation files from another package_ warning (#746). +#### Android +- Addresses an issue where bytes might be missing after first picking when `withData` is set to `true`. ([#616](https://github.com/miguelpruivo/flutter_file_picker/issues/616)). + +#### Desktop (GO) +- Patches README import path. (Thank you @voynichteru) + ## 3.0.2+2 - Fixes [#725](https://github.com/miguelpruivo/flutter_file_picker/issues/725). diff --git a/android/build.gradle b/android/build.gradle index 0c829aa..b3f0f0d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -22,7 +22,7 @@ rootProject.allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 29 + compileSdkVersion 30 defaultConfig { minSdkVersion 16 diff --git a/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java b/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java index bf86f8d..fb03d06 100644 --- a/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java +++ b/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java @@ -99,17 +99,8 @@ public class FileUtils { return true; } - public static FileInfo openFileStream(final Context context, final Uri uri, boolean withData) { - - Log.i(TAG, "Caching from URI: " + uri.toString()); - FileOutputStream fos = null; - final FileInfo.Builder fileInfo = new FileInfo.Builder(); - final String fileName = FileUtils.getFileName(uri, context); - final String path = context.getCacheDir().getAbsolutePath() + "/file_picker/" + (fileName != null ? fileName : new Random().nextInt(100000)); - - final File file = new File(path); - - if(file.exists() && withData) { + public static void loadData(final File file, FileInfo.Builder fileInfo) { + try { int size = (int) file.length(); byte[] bytes = new byte[size]; @@ -123,8 +114,23 @@ public class FileUtils { Log.e(TAG, "Failed to close file streams: " + e.getMessage(), null); } fileInfo.withData(bytes); - } else { + } catch (Exception e) { + Log.e(TAG, "Failed to load bytes into memory with error " + e.toString() + ". Probably the file is too big to fit device memory. Bytes won't be added to the file this time."); + } + } + + public static FileInfo openFileStream(final Context context, final Uri uri, boolean withData) { + + Log.i(TAG, "Caching from URI: " + uri.toString()); + FileOutputStream fos = null; + final FileInfo.Builder fileInfo = new FileInfo.Builder(); + final String fileName = FileUtils.getFileName(uri, context); + final String path = context.getCacheDir().getAbsolutePath() + "/file_picker/" + (fileName != null ? fileName : new Random().nextInt(100000)); + + final File file = new File(path); + + if(!file.exists()) { file.getParentFile().mkdirs(); try { fos = new FileOutputStream(path); @@ -139,19 +145,6 @@ public class FileUtils { out.write(buffer, 0, len); } - if(withData) { - try { - FileInputStream fis = null; - byte[] bytes = new byte[(int) file.length()]; - fis = new FileInputStream(file); - fis.read(bytes); - fis.close(); - fileInfo.withData(bytes); - } catch (Exception e) { - Log.e(TAG, "Failed to load bytes into memory with error " + e.toString() + ". Probably the file is too big to fit device memory. Bytes won't be added to the file this time."); - } - } - out.flush(); } finally { fos.getFD().sync(); @@ -170,6 +163,10 @@ public class FileUtils { Log.d(TAG, "File loaded and cached at:" + path); + if(withData) { + loadData(file, fileInfo); + } + fileInfo .withPath(path) .withName(fileName) diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart index 0b73330..7a662b4 100644 --- a/example/lib/generated_plugin_registrant.dart +++ b/example/lib/generated_plugin_registrant.dart @@ -4,7 +4,7 @@ // ignore_for_file: lines_longer_than_80_chars -import 'package:file_picker/src/file_picker_web.dart'; +import 'package:file_picker/_internal/file_picker_web.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; diff --git a/lib/src/file_picker_web.dart b/lib/_internal/file_picker_web.dart similarity index 98% rename from lib/src/file_picker_web.dart rename to lib/_internal/file_picker_web.dart index 43c52c5..76dec83 100644 --- a/lib/src/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -5,8 +5,8 @@ import 'dart:typed_data'; import 'package:file_picker/file_picker.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; -import 'file_picker_result.dart'; -import 'platform_file.dart'; +import '../src/file_picker_result.dart'; +import '../src/platform_file.dart'; class FilePickerWeb extends FilePicker { late Element _target; diff --git a/pubspec.yaml b/pubspec.yaml index d6329ce..6f4a843 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: file_picker description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker -version: 3.0.2+2 +version: 3.0.3 dependencies: flutter: @@ -26,4 +26,4 @@ flutter: pluginClass: FilePickerPlugin web: pluginClass: FilePickerWeb - fileName: src/file_picker_web.dart + fileName: _internal/file_picker_web.dart