Android11MIUI13中相机不工作,抛出CameraException(error,Attempttoinvokevirtualmethod'intandroid.hardware'
创始人
2024-08-20 11:00:35
0

通过在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 { CameraController _controller; Future _initializeControllerFuture;

@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( future: _initializeControllerFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { // If the Future is complete, display the preview. return CameraPreview(_controller); } else { // Otherwise, display a loading indicator. return Center(child: CircularProgressIndicator()); } }, ), floatingActionButton: FloatingActionButton( child: Icon(Icons.camera_alt), // Provide an onPressed callback. onPressed: () async { // Take the Picture in a try / catch block. If anything goes wrong, //

相关内容

热门资讯

第7分钟神器!微乐小程序免费黑... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
透视游戏!微乐小程序免费黑科技... 透视游戏!微乐小程序免费黑科技,微乐小程游戏破解器下载(透视)其实真的是有挂(哔哩哔哩)1、上手简单...
六分钟教程!微乐小程序黑科技,... 六分钟教程!微乐小程序黑科技,微乐家乡官方app(开挂)其实是真的挂(2026)1、许多玩家不知道微...
2026版规律!微乐小程序免费... 2026版规律!微乐小程序免费黑科技,广西微乐小程序辅助器(透视)一贯存在有挂(哔哩哔哩)1、该软件...
3分钟辅助!微乐小程序免费黑科... 3分钟辅助!微乐小程序免费黑科技,微信小程序微乐安徽辅助器(开挂)都是真的有挂(2026)暗藏猫腻,...
事发当天!微乐小程序黑科技,微... 事发当天!微乐小程序黑科技,微信小程序微乐安徽辅助苹果(透视)一直存在有挂(哔哩哔哩)1、很好的工具...
第二分钟安装!微乐小程序免费黑... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
黑科技技巧!微乐小程序免费黑科... 黑科技技巧!微乐小程序免费黑科技,微乐家乡官方app下载(透视)其实真的有挂(哔哩哔哩)1、打开软件...
9分钟脚本!微乐小程序黑科技,... 9分钟脚本!微乐小程序黑科技,微乐小程序微乐房间怎么开挂(开挂)真是是有挂(2026)1、玩家可以在...
昨日!微乐小程序免费黑科技,微... 昨日!微乐小程序免费黑科技,微信微乐辅助器免费安装(透视)果然是有挂(哔哩哔哩)1、用户打开应用后不...