[英]Flutter Image_Picker crash iOS app when open Camera
everything works perfect in Android when I open gallery or Camera, but when I try on iOS (emulator and real device) gallery works perfect but as soon as I press the Camera option the app crash without console logs only "Lost connection to device"当我打开图库或相机时,一切在 Android 中运行完美,但当我尝试 iOS(模拟器和真实设备)时,图库运行完美,但一旦我按下相机选项,应用程序崩溃而没有控制台日志,只有“与设备的连接丢失”
// get image File camera
_getFromCamera(BuildContext context) async {
XFile? pickedFile = await ImagePicker().pickImage(
source: ImageSource.camera,
maxWidth: 1800,
maxHeight: 1800,
);
File rotatedImage =
await FlutterExifRotation.rotateAndSaveImage(path: pickedFile!.path);
if (pickedFile != null) {
image = rotatedImage;
//File(pickedFile.path);
final userId = widget.id;
widget.bloc.uploadProfilePicture(image, userId);
}
}
Then I called the function in a Icon button
TextButton.icon(
icon: Icon(
Icons.photo_camera,
color: Theme.of(context).primaryColor,
),
label: Text(
AppLocalization.of(context)!
.getTranslatedValues("cameraLbl")!,
style: TextStyle(
color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold),
),
onPressed: () {
_getFromCamera(context);
Navigator.of(context).pop();
},
)
You have to add additional permissions inside the info.plist file您必须在 info.plist 文件中添加额外的权限
Add these entries inside it:在其中添加这些条目:
<key>NSPhotoLibraryUsageDescription</key>
<string>Needs PhotoLib access</string>
<key>NSCameraUsageDescription</key>
<string>Needs camera access</string>
To add permissions in flutter application, you need to add permissions in both android manifest and info.plist file.要在 flutter 应用程序中添加权限,您需要在 android 清单和 info.plist 文件中添加权限。
Also you can use any string inside the string tag, and that string would be shown to the user as why the user needs to give permission.您也可以在字符串标签内使用任何字符串,该字符串将作为用户需要授予权限的原因显示给用户。
Add the permisson keys inside the info.plist file (located in {app_folder}/ios/Runner/Info.plist在 info.plist 文件中添加权限密钥(位于 {app_folder}/ios/Runner/Info.plist
Here is the info.plist file located这是位于 info.plist 文件
and add并添加
<key>NSPhotoLibraryUsageDescription</key>
<string>Needs PhotoLib access</string>
<key>NSCameraUsageDescription</key>
<string>Needs camera access</string>
this will display the permission window for the camera and gallery这将显示相机和图库的权限 window
Hint: Hot reload is not enougth!提示:热重载是不够的! Press "Stop Debugging" and "Start Debugging"
按“停止调试”和“开始调试”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.