Reputation: 21
I currently try to get images from the Gallery to my App and save them, if not already there, in a sub folder in the Gallery. On Android everything works fine, when checking against the file name, image_picker returns. On iOS the returned name is always different ('image_pickerXXXXX.jpg').
After that I tried to hash the image itself. Here again, on Android I always get the same value, but on iOS the hash value is always different.
It seems, iOS does not only change the name of the file but also the byte value. If I am missing something here, I would be glad for suggestions.
My question is, does anyone know, if this is flutter related, where the behavior results from the image_picker implementation, or if this is a default behavior, iOS Gallery enforces.
Future<void> pickAPhoto() async {
// Here I get the original filename for Android, but something like 'image_pickerXXXXX.jpg' on iOS.
XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
File file = File(pickedFile.path);
List<int> imageBytes = await file.readAsBytes();
// This one is the same on Android for images that are original or copies, on iOS its always different.
Digest digest = sha256.convert(imageBytes);
Logger().i(digest);
File originalFile = File(pickedFile.path);
final dir = originalFile.parent;
String extension =
path.extension(originalFile.path);
final newPath = '${dir.path}/$digest$extension';
File renamedFile = await originalFile.rename(newPath);
addImageToArray(renamedFile);
inspection.images =
'${inspection.images}${path.basename(renamedFile.path)},';
inspection.isSynced = 0;
Inspection.updateInspection(inspection);
await saveFileToGallery(renamedFile.path);
}
}
The main reason I am asking is to determine if it would make sense for me to dive deep into the iOS Gallery API to write a plugin that works for my use case. I guess if it were that easy, or even possible, image_picker would have added an option, to get the original data, but maybe someone here knows more about that.
I tested my app on Android and iOS. I expected flutter image_picker to give back the same file or an exact copy of the picked file, when using Gallery as its source. Permissions seems to be fine.
<key>NSCameraUsageDescription</key>
<string>This app requires access to your camera to allow you to take photos for inspections and records. Photos can be stored locally and in the cloud for future reference.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs permission to save photos to your Photo Library. This feature is used when saving the photos of your inspections and records.</string>
I don't use my microphone anywhere, so I did not add this to the permissions. Maybe my question is a duplicate for this link:
Flutter image_picker don't save origin name
But the answer seems not to be related.
Thank you very much for your time.
Upvotes: 2
Views: 706