Skip to content

Commit

Permalink
[image_picker] Fix crash due to SecurityException (flutter#4004)
Browse files Browse the repository at this point in the history
Issue flutter/flutter#100025 mentions crashing of the image picker plugin due to a `SecurityException`. As research into the issue did not yield reproduction steps, we decided to surround the breaking method call with a `try/catch` block for now (see discussion in the issue). This PR implements just that. Instead of crashing on a `SecurityException`, the plugin will now return an image path of `null`.

This PR fixes flutter/flutter#100025.
  • Loading branch information
JeroenWeener authored and nploi committed Jul 16, 2023
1 parent bfd8555 commit 29827aa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6+16

* Fixes crashes caused by `SecurityException` when calling `getPathFromUri()`.

## 0.8.6+15

* Bumps androidx.activity:activity from 1.6.1 to 1.7.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ String getPathFromUri(final Context context, final Uri uri) {
// target file was written in full. Flushing the stream merely moves
// the bytes into the OS, not necessarily to the file.
return null;
} catch (SecurityException e) {
// Calling `ContentResolver#openInputStream()` has been reported to throw a
// `SecurityException` on some devices in certain circumstances. Instead of crashing, we
// return `null`.
//
// See https://github.com/flutter/flutter/issues/100025 for more details.
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
package io.flutter.plugins.imagepicker;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
Expand Down Expand Up @@ -66,7 +72,22 @@ public void FileUtil_GetPathFromUri() throws IOException {

assertTrue(bytes.length > 0);
String imageStream = new String(bytes, UTF_8);
assertTrue(imageStream.equals("imageStream"));
assertEquals("imageStream", imageStream);
}

@Test
public void FileUtil_GetPathFromUri_securityException() throws IOException {
Uri uri = Uri.parse("content://dummy/dummy.png");

ContentResolver mockContentResolver = mock(ContentResolver.class);
when(mockContentResolver.openInputStream(any(Uri.class))).thenThrow(SecurityException.class);

Context mockContext = mock(Context.class);
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);

String path = fileUtils.getPathFromUri(mockContext, uri);

assertNull(path);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Android implementation of the image_picker plugin.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22

version: 0.8.6+15
version: 0.8.6+16

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down

0 comments on commit 29827aa

Please sign in to comment.