Skip to content

[image_picker][android] Non-bitmap images now return path instead of null #3590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.7+3

* Adds handling of unsupported image types to the example.

## 0.8.7+2

* Updates minimum Flutter version to 3.3.
Expand Down
8 changes: 7 additions & 1 deletion packages/image_picker/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ class _MyHomePageState extends State<MyHomePage> {
label: 'image_picker_example_picked_image',
child: kIsWeb
? Image.network(_imageFileList![index].path)
: Image.file(File(_imageFileList![index].path)),
: Image.file(
File(_imageFileList![index].path),
errorBuilder: (BuildContext context, Object error,
StackTrace? stackTrace) =>
const Center(
child: Text('This image type is not supported')),
),
);
},
itemCount: _imageFileList!.length,
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.7+2
version: 0.8.7+3

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.8.6+7

* Fixes handling of non-bitmap image types.
* Updates minimum Flutter version to 3.3.

## 0.8.6+6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ private void launchPickImageFromGalleryIntent(Boolean useAndroidPhotoPicker) {
pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickImageIntent.setType("image/*");
}

activity.startActivityForResult(pickImageIntent, REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ String resizeImageIfNeeded(
String imagePath, @Nullable Double maxWidth, @Nullable Double maxHeight, int imageQuality) {
Bitmap bmp = decodeFile(imagePath);
if (bmp == null) {
return null;
return imagePath;
}
boolean shouldScale = maxWidth != null || maxHeight != null || imageQuality < 100;
if (!shouldScale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -16,6 +17,8 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

Expand All @@ -26,6 +29,7 @@ public class ImageResizerTest {

ImageResizer resizer;
File imageFile;
File svgImageFile;
File externalDirectory;
Bitmap originalImageBitmap;

Expand All @@ -35,6 +39,7 @@ public class ImageResizerTest {
public void setUp() throws IOException {
mockCloseable = MockitoAnnotations.openMocks(this);
imageFile = new File(getClass().getClassLoader().getResource("pngImage.png").getFile());
svgImageFile = new File(getClass().getClassLoader().getResource("flutter_image.svg").getFile());
originalImageBitmap = BitmapFactory.decodeFile(imageFile.getPath());
TemporaryFolder temporaryFolder = new TemporaryFolder();
temporaryFolder.create();
Expand Down Expand Up @@ -78,4 +83,23 @@ public void onResizeImageIfNeeded_whenParentDirectoryDoesNotExists_shouldNotCras
String outputFile = invalidResizer.resizeImageIfNeeded(imageFile.getPath(), null, 50.0, 100);
assertThat(outputFile, equalTo(nonExistentDirectory.getPath() + "/scaled_pngImage.png"));
}

@Test
public void onResizeImageIfNeeded_whenImagePathIsNotBitmap_shouldReturnPathAndNotNull() {
String nonBitmapImagePath = svgImageFile.getPath();

// Mock the static method
try (MockedStatic<BitmapFactory> mockedBitmapFactory =
Mockito.mockStatic(BitmapFactory.class)) {
// Configure the method to return null when called with a non-bitmap image
mockedBitmapFactory
.when(() -> BitmapFactory.decodeFile(nonBitmapImagePath, null))
.thenReturn(null);

String resizedImagePath = resizer.resizeImageIfNeeded(nonBitmapImagePath, null, null, 100);

assertNotNull(resizedImagePath);
assertThat(resizedImagePath, equalTo(nonBitmapImagePath));
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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+6
version: 0.8.6+7

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