Download Image from URL in Flutter

code to download image from url in and store it on devices storage

Hey there in this article i am sharing you step by step guide for making image download option in your flutter application. There are many ways you can achieve this but i am showing the most simple way using ready made plugin.

lets get started. before moving on i am assuming you have already running project in hand. now follow me.

step 1 : Import plugin

Open pubspec.yaml file and add this plugin and run flutter pub get command or if you are using vs code simple save (crtl + s) and wait till flutter downloads required files.

  image_downloader: ^0.30.0 to ready document click here

  image_downloader: ^0.30.0

After running pub time to add permission in menifieast.xml file

step 2: Add Permission in AndroidManifest.xml

Open your anroid project and navigate to AndroidManifest.xml file

in side AndroidManifest.xml file add these 2 permissions

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

after adding this permissions save and close the AndroidManifest.xml file.

step 3 : create download function

after adding permissions to AndroidManifest.xml file now it is time to make download function and save image from url to users device storage.

first import the image downloader library file and use donloadImage() function of ImageDownloader class. pass image url as parameter and rest of the download work library will do by it self that it.

code snippet :

Future<void> downloadImage() async {
    try {
      var imageId = await ImageDownloader.downloadImage(url).then((value) {
        messageDialog(
            context, "Download Success.", "Image Saved to downloads.");
      }).onError((error, stackTrace) {});

      //var path = await ImageDownloader.findPath(imageId);
    } on PlatformException catch (error) {
      print(error);
    }
  }

That is it run the project and see your image download function running on live device as well as emulator.

Thanks for reading. Enjoy Coding

Raj Avatar