hoangquyy
hoangquyy

Reputation: 2073

How to save image in Image widget to android device?

I have an Image widget that contains an image and I want to save that image to Android device for later use.

I found this topic but I can't find out the way to convert Image to File.

So anyone know how to convert Image to File or directly save image from Image widget to Android device in Flutter.

Thanks you.

Upvotes: 1

Views: 288

Answers (1)

Gursewak Singh
Gursewak Singh

Reputation: 1978

Try with it image_picker_saver

Or

You need to save the image into external storage directory for showing the image on gallery. Instead of getting temporary directory, obtain external storage directory.

final directory = await getExternalStorageDirectory();

You need to provide the permission on AndroidManifest.xml file of your android/app/src/main folder

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

then let's say that you want to create a folder named MyImages and add the new image to that folder,

final myImagePath = '${directory.path}/MyImages' ;
final myImgDir = await new Directory(myImagePath).create();

then write to the file to the path.

var kompresimg = new File("$myImagePath/image_$baru$rand.jpg")
  ..writeAsBytesSync(img.encodeJpg(gambarKecilx, quality: 95));

for getting the number of files, just obtain the files to a list and check the length of the list

var listOfFiles = await myImgDir.list(recursive: true).toList();
var count = countList.length;

Upvotes: 2

Related Questions