我正在学习如何从图库中选择图像。我已经实现了同样的颤振图像选择器。但是当我尝试在模拟器中选择图像时,我无法这样做。
请帮忙
颤动医生
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 1.26.0-17.3.pre, on macOS 11.1 20C69 darwin-arm, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Android Studio (version 4.1)
[✓] Connected device (1 available)
• No issues found!
我的代码
import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MaterialApp(
home: Play(),
// theme: ThemeData.dark(),
));
}
class Play extends StatefulWidget {
@override
_PlayState createState() => _PlayState();
}
class _PlayState extends State<Play> {
File _file;
ImagePicker _picker = ImagePicker();
PickedFile _pickedFile;
Future _getImageFromGallery() async {
print("Getting Image from Gallery.");
_pickedFile = await _picker.getImage(source: ImageSource.gallery);
print(_pickedFile.path);
setState(() {
_file = File(_pickedFile.path);
});
}
Future _getImageFromCamera() async {
print("Getting Image from Camera.");
_pickedFile = await _picker.getImage(source: ImageSource.camera);
setState(() {
_file = File(_pickedFile.path);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Play"),
),
body: Column(
children: [
Center(
child: ElevatedButton(
onLongPress: _getImageFromCamera,
onPressed: _getImageFromGallery,
child: Text("Get Image")),
),
_file != null
? Image.file(
_file,
height: 200,
)
: Icon(
Icons.image,
size: 100,
),
],
),
);
}
}
我使用的是M1 Mac Air。我已切换到测试版频道以消除一些详细的错误。 一切正常。只是,当我点击图像时什么也没有发生。没有被选中。
我在 iOS 17 上遇到了同样的问题。我通过将以下行添加到 info.plist 来修复它
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to pick an image for your profile.</string>
如果您正在访问相机,您还需要添加这些
<key>NSPhotoLibraryUsageDescription</key>
<string>Need to upload image</string>
<key>NSCameraUsageDescription</key>
<string>Need to upload image</string>
<key>NSMicrophoneUsageDescription</key>
<string>Need to upload image</string>