我的新安卓应用程序在运行image_picker时崩溃(基于颤振飞镖)



我正在开发一个使用 Flutter 购物的应用程序,所以我正在某个地方堆叠,我需要帮助。

我使用 pub.dev (https://pub.dev/packages/image_picker#-readme-tab-( 上提供的image_picker代码,然后开发了一个用于在我的应用程序上添加产品的页面,因此当我单击相机图标或图库图标以选择图像时,应用程序崩溃并打开相机屏幕/图库屏幕。

我最大的问题是; 它与模拟器一起工作,但在真正的手机上,它会崩溃。

我尝试使用 pub.dev 上所示的 retrieveLostData((,但我不知道在哪里使用它。

波纹管是我的代码

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
//my imports
import 'package:myshop/main.dart';
class ImageCapture extends StatefulWidget {
@override
_ImageCaptureState createState() => _ImageCaptureState();
}
class _ImageCaptureState extends State<ImageCapture> {
File _imageFile;
Future<void> _cropImage() async {
File cropped = await ImageCropper.cropImage(
sourcePath: _imageFile.path,);
setState(() {
_imageFile = cropped ?? _imageFile;
});
}
// pick image from galery or camera
Future<void> _pickImage(ImageSource source) async {
File selected = await ImagePicker.pickImage(source: source);
setState(() {
_imageFile = selected;
});
}
//remove image
void _clear() {
setState(() => _imageFile = null);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.0,
backgroundColor: Colors.orange[700],
title: Text(
'Image Capture',
style: TextStyle(fontFamily: 'Exo', fontSize: 13.0),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
size: 23.0,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.home,
size: 18,
color: Colors.white,
),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Home())))
],
),
//pick image from camera or gallery
bottomNavigationBar: BottomAppBar(
color: Colors.cyan[900],
child: Container( margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.photo_camera, color: Colors.orange, size: 18,),
onPressed: () => _pickImage(ImageSource.camera),
),
IconButton(
icon: Icon(Icons.photo_library, color: Colors.orange, size: 18,),
onPressed: () => _pickImage(ImageSource.gallery),
),
],
),
),
),
body: ListView(
children: <Widget>[
if (_imageFile != null) ...[
Image.file(_imageFile),
Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
child: Icon(Icons.crop, size: 18,),
onPressed: _cropImage,
),
FlatButton(
child: Icon(Icons.refresh,size: 18,),
onPressed: _clear,
),
],
),
], if (_imageFile==null)...[
Center(
child: Text('No Image Captured', style: TextStyle(color: Colors.black54),),
)
]
],
),
);
}
}

您需要在项目的AndroidManifest.xml文件中添加camerastorage permission

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

此外,您需要在活动中检查两者的运行时权限。

前往棉花糖中的存储权限错误以获取更多信息。

最新更新