當我在模擬器中使用 image_picker 時,它看起來不錯,當它按下時它可以正常作業,但是當我將它安裝到我的手機時,當我按下 image_picker 的按鈕時它什么也不做,沒有相機顯示任何變化。
我認為這必須是一些許可”的事情,但我不太明白
方法:
var selectedImagePath = ''.obs;
getImage(ImageSource ImageSource) async {
final pickedFile = await ImagePicker().pickImage(source: ImageSource);
if (pickedFile != null) {
selectedImagePath.value = pickedFile.path;
} else {
Get.snackbar('Error', 'No Image Selected');
}
}
展示:
Scaffold(
body: SafeArea(
child: Column(
children: [
Obx(
() => Container(
width: double.infinity,
height: 400,
decoration: selectedImagePath == ''
? BoxDecoration(
color: Colors.grey,
)
: BoxDecoration(
color: Colors.grey,
image: DecorationImage(
image: FileImage(
File(
selectedImagePath.value,
),
),
fit: BoxFit.cover
),
),
),
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
getImage(ImageSource.camera);
},
child: Text('Take a photo'),
),
ElevatedButton(
onPressed: () {
getImage(ImageSource.gallery);
},
child: Text('Take from gallery'),
),
],
),
],
),
),
);
uj5u.com熱心網友回復:
這對我有用
File? _imageFile;
Future<void> getImage(
ImageSource source) async {
XFile? pickedFile = await ImagePicker()
.pickImage(source: source);
if (pickedFile != null) {
File selected = File(pickedFile.path);
_imageFile = selected;
}
}
請讓我知道這對你有沒有用。
uj5u.com熱心網友回復:
這是我的代碼,我使用 Provider Library 來管理它。
步驟1:
//File? _image;
String? imgString;
ImagePicker? imagePicker = ImagePicker();
第 2 步:小部件代碼
Future getImage() async {
final dynamic image =
await imagePicker?.pickImage(source: ImageSource.gallery);
setState(() {
if (image != null) {
YourProvider.myImage = File(image.path);
YourProvider.pickedImage = true;
imgString = YourBaseUrl YourProvider.myImage!.path;
debugPrint("Image: $imgString");
}
});
}
第 3 步:提供者
bool pickedImage = false;
File? myImage;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386109.html
上一篇:RangeError(RangeError(index):Invalidvalue:Notininclusiverange0..1:2)使用兩個以上擴展面板串列時抖動
