在我的应用程序中,我正在拍照并将其存储在设备中,还使用 sqlite 存储路径。现在,如果我使用下面给出的代码删除数据,它会从本地数据库(sqlite)中删除,但不要从文件中删除图片。例如,我创建了 3 个事务并删除了其中的 2 个,但在文件中我仍然有 3 个图像(在文件中)。那么如何删除它们。请帮忙 。
保存:
Future _takeImage() async {
PickedFile imageFile = await ImagePicker().getImage(
source: ImageSource.camera,
maxWidth: 600,
);
_saveImg(imageFile);
}
Future _saveImg(PickedFile imageFile) async {
if (imageFile == null) {
return;
}
setState(() {
_storedImage = File(imageFile.path);
});
final appDirectory = await sysPath.getApplicationDocumentsDirectory();
final fileName =
path.basename(_storedImage.path); //name of the photo created by camera.
final savedImage = await _storedImage
.copy('${appDirectory.path}/$fileName'); // storing image.
widget.onSelectImage(savedImage.path);
}
删除:
Future<void> deleteExpanse(int id) async {
try {
final MyDatabase dbManager = MyDatabase();
await dbManager.deleteTransaction(id, "Expense");
DataSample temp = _expenseItems.firstWhere((element) => id == element.id);
await _deleteImage(temp);
_expenseItems.removeWhere((element) => element.id == id);
} catch (error) {
throw error;
}
notifyListeners();
}
_deleteImage(DataSample data )async {
final directory = await getApplicationDocumentsDirectory();
final path = join(directory.path, data.image );
bool isExist = await File(path).exists();
if (isExist) {
await File(path).delete();
}
}
详细代码:从这里拍摄或选择图片并将其传递给方法 =>
class ImageInput extends StatefulWidget {
final Function onSelectImage;
final String imageFile;
ImageInput(this.onSelectImage, this.imageFile);
@override
_ImageInputState createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File _storedImage;
Future _choseImage() async {
try {
PickedFile imageFile = await ImagePicker().getImage(
source: ImageSource.gallery,
maxWidth: 600,
);
_saveImg(imageFile);
} catch (error) {
throw error;
}
}
Future _takeImage() async {
try {
PickedFile imageFile = await ImagePicker().getImage(
source: ImageSource.camera,
maxWidth: 600,
);
_saveImg(imageFile);
} catch (error) {
throw error;
}
}
Future _saveImg(PickedFile imageFile) async {
try {
if (imageFile == null) {
return;
}
bool a = await File(imageFile.path).exists();
setState(() {
if(a){
_storedImage = File(imageFile.path);
}
});
final appDirectory = await sysPath.getApplicationDocumentsDirectory();
final fileName = path
.basename(_storedImage.path); //name of the photo created by camera.
final savedImage = await _storedImage
.copy('${appDirectory.path}/$fileName'); // storing image.
widget.onSelectImage(savedImage.path);
} catch (error) {
throw error;
}
}
@override
void initState() {
// TODO: implement initState
_storedImage = widget.imageFile.isEmpty ? null : File(widget.imageFile);
super.initState();
}
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.24,
width: MediaQuery.of(context).size.width * 0.5,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(width: 1, color: Color(0xFF495464)),
),
child: _storedImage != null
? Image.file(
_storedImage,
fit: BoxFit.cover,
width: double.infinity,
)
: Text(
"No Image Chosen",
style: GoogleFonts.courgette(
fontSize: MediaQuery.of(context).size.width * 0.05),
),
alignment: Alignment.center,
),
SizedBox(width: 10),
Expanded(
child: FittedBox(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FlatButton.icon(
icon: Icon(
Icons.camera,
color: Color(0xFF495464),
),
label: Text(
'Take Picture',
style: TextStyle(color: Color(0xFF495464), fontSize: 20),
),
onPressed: _takeImage,
),
FlatButton.icon(
icon: Icon(
Icons.photo_library,
color: Color(0xFF495464),
),
label: Text(
'Choose Picture',
style: TextStyle(color: Color(0xFF495464), fontSize: 20),
),
onPressed: _choseImage,
),
],
),
),
),
],
);
}
}
它来到这里然后我将它传递给上面给出的提供者删除方法(我只给出上面的费用删除方法)=>
var _initState = true;
var trData;
String initialPrice = '';
var _newTransaction = DataSample(
id: null,
datetime: null,
title: '',
image: '',
price: 0.0,
description: '',
);
@override
void didChangeDependencies() {
if (_initState) {
trData = ModalRoute.of(context).settings.arguments as Map;
if (trData['id'] != 0) {
if (trData['type'] == "Expense") {
_newTransaction = Provider.of<ExpanseProvider>(context, listen: false)
.findId(trData['id']);
} else {
_newTransaction = Provider.of<IncomeProvider>(context, listen: false)
.findId(trData['id']);
}
_selectedDate = _newTransaction.datetime;
initialPrice = _newTransaction.price.toString();
}
}
_initState = false;
super.didChangeDependencies();
}
final _gKey = GlobalKey<FormState>();
DateTime _selectedDate = DateTime.now();
String _pickedImage = '';
void _selectImage(String pickedImage) {
_pickedImage = pickedImage;
}
void _saveInput(String page) {
final _isValid = _gKey.currentState.validate();
if (!_isValid) {
return;
}
_gKey.currentState.save();
_newTransaction = DataSample(
title: _newTransaction.title,
datetime: _selectedDate,
image: _pickedImage.isEmpty ? _newTransaction.image : _pickedImage,
id: _newTransaction.id,
price: _newTransaction.price,
description: _newTransaction.description,
);
deleteOrUpdate(page);
}
Future<void> deleteOrUpdate(String page) async {
if (_newTransaction.id == null) {
if (page == 'Expense') {
await Provider.of<ExpanseProvider>(context, listen: false)
.addExpanseTransaction(_newTransaction)
.then((value) => Navigator.of(context).pop())
.catchError((error) {
return _onError();
});
} else {
await Provider.of<IncomeProvider>(context, listen: false)
.addIncomeTransaction(_newTransaction)
.then((value) => Navigator.of(context).pop())
.catchError((error) {
return _onError();
});
}
} else {
if (page == 'Expense') {
await Provider.of<ExpanseProvider>(context, listen: false)
.updateExpense(_newTransaction)
.then((value) => Navigator.of(context).pop())
.catchError((error) {
return _onError();
});
} else {
await Provider.of<IncomeProvider>(context, listen: false)
.updateIncome(_newTransaction)
.then((value) => Navigator.of(context).pop())
.catchError((error) {
return _onError();
});
}
}
}