Skip to content

Commit 103cf0f

Browse files
authored
Ensure we fail if we cannot close file (flutter#615)
1 parent bdb5a8d commit 103cf0f

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

packages/image_picker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.6
2+
3+
* Added support for picking remote images.
4+
15
## 0.4.5
26

37
* Bugfixes, code cleanup, more test coverage.

packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/FileUtils.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import android.provider.MediaStore;
3131
import android.text.TextUtils;
3232
import java.io.File;
33-
import java.io.FileNotFoundException;
3433
import java.io.FileOutputStream;
3534
import java.io.IOException;
3635
import java.io.InputStream;
@@ -129,22 +128,19 @@ private static String getDataColumn(
129128
}
130129

131130
private static String getPathFromRemoteUri(final Context context, final Uri uri) {
131+
// The code below is why Java now has try-with-resources and the Files utility.
132+
File file = null;
132133
InputStream inputStream = null;
133134
OutputStream outputStream = null;
135+
boolean success = false;
134136
try {
135137
inputStream = context.getContentResolver().openInputStream(uri);
136-
File file = File.createTempFile("cachedImage", "jpg", context.getCacheDir());
138+
file = File.createTempFile("image_picker", "jpg", context.getCacheDir());
137139
outputStream = new FileOutputStream(file);
138140
if (inputStream != null) {
139-
byte[] buffer = new byte[4 * 1024];
140-
int read;
141-
while ((read = inputStream.read(buffer)) != -1) {
142-
outputStream.write(buffer, 0, read);
143-
}
144-
outputStream.flush();
145-
return file.getPath();
141+
copy(inputStream, outputStream);
142+
success = true;
146143
}
147-
} catch (FileNotFoundException ignored) {
148144
} catch (IOException ignored) {
149145
} finally {
150146
try {
@@ -154,9 +150,22 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
154150
try {
155151
if (outputStream != null) outputStream.close();
156152
} catch (IOException ignored) {
153+
// If closing the output stream fails, we cannot be sure that the
154+
// target file was written in full. Flushing the stream merely moves
155+
// the bytes into the OS, not necessarily to the file.
156+
success = false;
157157
}
158158
}
159-
return null;
159+
return success ? file.getPath() : null;
160+
}
161+
162+
private static void copy(InputStream in, OutputStream out) throws IOException {
163+
final byte[] buffer = new byte[4 * 1024];
164+
int bytesRead;
165+
while ((bytesRead = in.read(buffer)) != -1) {
166+
out.write(buffer, 0, bytesRead);
167+
}
168+
out.flush();
160169
}
161170

162171
private static boolean isExternalStorageDocument(Uri uri) {

packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,15 @@ private void handleImageResult(String path) {
454454
String finalImagePath = imageResizer.resizeImageIfNeeded(path, maxWidth, maxHeight);
455455
finishWithSuccess(finalImagePath);
456456
} else {
457-
throw new IllegalStateException("Received images from picker that were not requested");
457+
throw new IllegalStateException("Received image from picker that was not requested");
458458
}
459459
}
460460

461461
private void handleVideoResult(String path) {
462462
if (pendingResult != null) {
463463
finishWithSuccess(path);
464464
} else {
465-
throw new IllegalStateException("Received images from picker that were not requested");
465+
throw new IllegalStateException("Received video from picker that was not requested");
466466
}
467467
}
468468

packages/image_picker/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors:
55
- Flutter Team <flutter-dev@googlegroups.com>
66
- Rhodes Davis Jr. <rody.davis.jr@gmail.com>
77
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
8-
version: 0.4.5
8+
version: 0.4.6
99

1010
flutter:
1111
plugin:

0 commit comments

Comments
 (0)