1

I'm creating a flutter app which hide your photos. I've used image_picker package to select image form gallery. And successfully copied the image to my application storage. Now I wanna delete original image from the gallery. How I can do that?


import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';


final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
final File imageFile = File(pickedFile.path);
final fileName = DateTime.now().millisecondsSinceEpoch.toString();
final newPath = '${appDir.path}/$fileName.jpg';
final newFile = await File(pickedFile.path).copy(newPath);

await File(pickedFile.path).delete();

Image is not deleting as pickedFile.path is returning cache image path not the original path of selected image.

I've tried many code snippets from the ChatGPT and other blogs. Almost every solution was same but it didn't work in my case.

import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';


final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
final File imageFile = File(pickedFile.path);
final fileName = DateTime.now().millisecondsSinceEpoch.toString();
final newPath = '${appDir.path}/$fileName.jpg';
final newFile = await File(pickedFile.path).copy(newPath);

await File(pickedFile.path).delete();

Above is the sample code. In this code:

await File(pickedFile.path).delete();

pickedFile.path is not returning original path. It's the path of cached image which user has selected.

How I can delete selected images from gallery?

Mr Sindhu
  • 316
  • 2
  • 11
  • If you want to access the user's phone files and delete it from there, its going to be a lot of code work. Its a big project. For starters learn to use the file_manager package: https://pub.dev/packages/file_manager – Texv Mar 31 '23 at 06:48
  • 1
    @Texv Thank you for your response on my StackOverflow question. Your solution seems promising, although a bit lengthy. I appreciate your effort and will work on implementing it. Also suggest any package which reduce my coding efforts if you've in your knowledge. Thanks again! – Mr Sindhu Mar 31 '23 at 07:01

2 Answers2

0

i suggest you using Photo Manager PhotoManager package delete photo

0

Hello for this purpose i used wechat_assets_picker: ^8.4.3 this plugin it awesome plugin works fine but issue that i test on android 29 it not working well

joshua
  • 177
  • 1
  • 2
  • 14