Skip to content

Commit

Permalink
Fixes #616 and #746
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ruivo committed Jun 22, 2021
1 parent 0dc87e9 commit 9f0a811
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,8 @@ public static boolean clearCache(final Context context) {
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];

Expand All @@ -123,8 +114,23 @@ public static FileInfo openFileStream(final Context context, final Uri uri, bool
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);
Expand All @@ -139,19 +145,6 @@ public static FileInfo openFileStream(final Context context, final Uri uri, bool
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();
Expand All @@ -170,6 +163,10 @@ public static FileInfo openFileStream(final Context context, final Uri uri, bool

Log.d(TAG, "File loaded and cached at:" + path);

if(withData) {
loadData(file, fileInfo);
}

fileInfo
.withPath(path)
.withName(fileName)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -26,4 +26,4 @@ flutter:
pluginClass: FilePickerPlugin
web:
pluginClass: FilePickerWeb
fileName: src/file_picker_web.dart
fileName: _internal/file_picker_web.dart

0 comments on commit 9f0a811

Please sign in to comment.