通过在AndroidManifest.xml文件中声明CAMERA权限并重新安装应用程序来解决此问题。以下是包含CAMERA权限声明和初始化相机的示例代码:
在AndroidManifest.xml文件中添加以下权限声明:
在您的Flutter代码中,使用以下代码初始化相机:
import 'package:camera/camera.dart';
void main() async { WidgetsFlutterBinding.ensureInitialized();
final cameras = await availableCameras(); final camera = cameras.first;
runApp( MaterialApp( theme: ThemeData.dark(), home: TakePictureScreen( // Pass the appropriate camera to the TakePictureScreen widget. camera: camera, ), ), ); }
class TakePictureScreen extends StatefulWidget { final CameraDescription camera;
const TakePictureScreen({ Key key, @required this.camera, }) : super(key: key);
@override TakePictureScreenState createState() => TakePictureScreenState(); }
class TakePictureScreenState extends State
@override void initState() { super.initState(); // To display the current output from the Camera, // create a CameraController. _controller = CameraController( // Get a specific camera from the list of available cameras. widget.camera, // Define the resolution to use. ResolutionPreset.high, );
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
@override void dispose() { // Dispose of the controller when the widget is disposed. _controller.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Take a picture')),
// Wait until the controller is initialized before displaying the
// camera preview. Use a FutureBuilder to display a loading spinner
// until the controller has finished initializing.
body: FutureBuilder