Skip to content

Commit

Permalink
add support using without adding permissions (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: NGhebreial <[email protected]>
Co-authored-by: Anton Dudakov <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2021
1 parent 48e52bd commit 0f8f8a4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add `flutter_exif_rotation` as a dependency in your `pubsec.yaml`

### Android

Add this in your `AndroidManifest.xml`
Add this in your `AndroidManifest.xml` (you don't need these permissions in case when you manipulate files from internal storage only (file are stored inside the app's directory ( /data/data/{your package}/ )) )

```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,26 @@ public boolean onRequestPermissionsResult(int requestCode, String[] permissions,


public void rotateImage() {
Boolean internalFile = argument(call, "internalFile", false);

if (!internalFile
&& (!permissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE)
|| !permissionManager.isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE))) {

permissionManager.askForPermission(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_IMAGE_STORAGE_PERMISSION
);

if (!permissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE) || !permissionManager.isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
permissionManager.askForPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_IMAGE_STORAGE_PERMISSION);
return;
}
launchRotateImage();

}

public void launchRotateImage() {
private void launchRotateImage() {
String photoPath = call.argument("path");
Boolean save = call.argument("save");
Boolean save = argument(call, "save", false);

int orientation = 0;
try {
Expand Down Expand Up @@ -213,4 +221,11 @@ private static Bitmap rotate(Bitmap source, float angle) {
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

private static <T> T argument(MethodCall call, String key, T defaultValue) {
if (!call.hasArgument(key)) {
return defaultValue;
}

return call.argument(key);
}
}
52 changes: 41 additions & 11 deletions lib/flutter_exif_rotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,55 @@ class FlutterExifRotation {
}

/// Get the [path] of the image and fix the orientation.
/// [internalFile] - on Android if a file stored inside the app's
/// directory ( /data/data/{your package}/ ) you don't need the
/// READ/WRITE_EXTERNAL_STORAGE permission to manipulate with it.
/// So if you will not use files from external storage set this param to
/// true. In this case you can remove READ/WRITE_EXTERNAL_STORAGE
/// permissions from the AndroidManifest
/// Return the [File] with the exif data fixed
static Future<File> rotateImage({required String path}) async {
assert(path != null);
String filePath = await (_channel.invokeMethod(
'rotateImage',
<String, dynamic>{'path': path, 'save': false},
));

return new File(filePath);
}
static Future<File> rotateImage({
required String path,
bool internalFile = false,
}) async =>
await _rotateImageInternal(
path: path,
save: false,
internalFile: internalFile,
);

/// Get the [path] of the image, fix the orientation and
/// saves the file in the device.
/// [internalFile] - on Android if a file stored inside the app's
/// directory ( /data/data/{your package}/ ) you don't need the
/// READ/WRITE_EXTERNAL_STORAGE permission to manipulate with it.
/// So if you will not use files from external storage set this param to
/// true. In this case you can remove READ/WRITE_EXTERNAL_STORAGE
/// permissions from the AndroidManifest
/// Return the [File] with the exif data fixed
static Future<File> rotateAndSaveImage({required String path}) async {
static Future<File> rotateAndSaveImage({
required String path,
bool internalFile = false,
}) async =>
await _rotateImageInternal(
path: path,
save: true,
internalFile: internalFile,
);

static Future<File> _rotateImageInternal({
required String path,
required bool save,
required bool internalFile,
}) async {
assert(path != null);
String filePath = await (_channel.invokeMethod(
'rotateImage',
<String, dynamic>{'path': path, 'save': true},
<String, dynamic>{
'path': path,
'save': false,
'internalFile': internalFile
},
));

return new File(filePath);
Expand Down

0 comments on commit 0f8f8a4

Please sign in to comment.