Flutter - 在真实设备中安装后图像选择器不工作
Flutter - Image Picker not Working After Installing in Real Device
我的图像选择器在我的真实设备的调试模式下在模拟器 7 中工作正常,但在我的真实设备中导出应用程序后无法工作..
我正在使用这个plugin
这个视频有什么问题:
Video in Google Drive:
在调试中一切正常:
video in google drive
如您所见,在调试模式下一切正常
这是我的图像选择功能
File userAvatar;
File get getUserAvatar => userAvatar;
Future pickUserAvatar(BuildContext context, ImageSource source) async {
final pickedUserAvatar = await picker.getImage(source: source);
BotToast.showText(text: 'my picked name is $source');
pickedUserAvatar == null
? BotToast.showText(text: 'Select Image')
: userAvatar = File(pickedUserAvatar.path);
userAvatar != null
? Provider.of<LandingService>(context, listen: false)
.showUserAvatar(context)
: BotToast.showText(text: 'Image Upload Error');
notifyListeners();
}
这是我的show user头像功能 我在图片成功选取后显示的地方
showUserAvatar(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
CircleAvatar(
radius: 80.0,
backgroundColor: constantColors.transperant,
backgroundImage: FileImage(
Provider.of<LandingUtils>(context, listen: false)
.userAvatar,
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
child: Text(
'Reselect',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationColor: constantColors.whiteColor,
),
),
onPressed: () {
Provider.of<LandingUtils>(context, listen: false)
.pickUserAvatar(context, ImageSource.gallery);
},
),
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Confirm Image',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
Provider.of<FirebasessOperations>(context,
listen: false)
.uploadUserAvatar(context)
.whenComplete(() => {
signUpSheet(context),
});
},
)
],
),
)
],
),
),
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.circular(15.0),
),
);
});
}
我也在我的 android 清单中添加了这一行
android:requestLegacyExternalStorage="true"
您需要先检查并授予用户权限,然后再执行照片选择功能
请看看这个插件:https://pub.dev/packages/permission_handler
示例:
// request a user for permission first
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.camera,
].request();
if (await Permission.storage.request().isGranted) {
// Either the permission was already granted before or the user just granted it you
can now select photo.
}
问题是您的构建 Gradle 版本
转到 android/build.grale
classpath 'com.android.tools.build:gradle:3.5.4'
希望有用,
我的图像选择器在我的真实设备的调试模式下在模拟器 7 中工作正常,但在我的真实设备中导出应用程序后无法工作..
我正在使用这个plugin
这个视频有什么问题: Video in Google Drive:
在调试中一切正常:
video in google drive 如您所见,在调试模式下一切正常
这是我的图像选择功能
File userAvatar;
File get getUserAvatar => userAvatar;
Future pickUserAvatar(BuildContext context, ImageSource source) async {
final pickedUserAvatar = await picker.getImage(source: source);
BotToast.showText(text: 'my picked name is $source');
pickedUserAvatar == null
? BotToast.showText(text: 'Select Image')
: userAvatar = File(pickedUserAvatar.path);
userAvatar != null
? Provider.of<LandingService>(context, listen: false)
.showUserAvatar(context)
: BotToast.showText(text: 'Image Upload Error');
notifyListeners();
}
这是我的show user头像功能 我在图片成功选取后显示的地方
showUserAvatar(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
CircleAvatar(
radius: 80.0,
backgroundColor: constantColors.transperant,
backgroundImage: FileImage(
Provider.of<LandingUtils>(context, listen: false)
.userAvatar,
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
child: Text(
'Reselect',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationColor: constantColors.whiteColor,
),
),
onPressed: () {
Provider.of<LandingUtils>(context, listen: false)
.pickUserAvatar(context, ImageSource.gallery);
},
),
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Confirm Image',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
Provider.of<FirebasessOperations>(context,
listen: false)
.uploadUserAvatar(context)
.whenComplete(() => {
signUpSheet(context),
});
},
)
],
),
)
],
),
),
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.circular(15.0),
),
);
});
}
我也在我的 android 清单中添加了这一行
android:requestLegacyExternalStorage="true"
您需要先检查并授予用户权限,然后再执行照片选择功能 请看看这个插件:https://pub.dev/packages/permission_handler 示例:
// request a user for permission first
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.camera,
].request();
if (await Permission.storage.request().isGranted) {
// Either the permission was already granted before or the user just granted it you
can now select photo.
}
问题是您的构建 Gradle 版本 转到 android/build.grale
classpath 'com.android.tools.build:gradle:3.5.4'
希望有用,