Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can save an image from the internet to a local directory using Flutter by using the dio package to download the image and the path_provider package to get the local app directory. Here's an example of how to do it:

First, add the dio and path_provider packages to your pubspec.yaml file:

dependencies:
  dio: ^4.0.0
  path_provider: ^2.0.1

Then import them in your Dart file:

import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

Next, use the following code to download the image and save it to a local directory:

void saveImage(String imageUrl) async {
  try {
    Dio dio = Dio();
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String savePath = appDocDir.path + "/image.jpg";
    await dio.download(imageUrl, savePath);
    print("Image saved to local directory");
  } catch (e) {
    print(e.toString());
  }
}

Replace imageUrl with the URL of the image you want to download. The getApplicationDocumentsDirectory() method gets the directory where your app can store files. savePath is the path to the file where the image will be saved. Finally, the dio.download() method downloads the image from the URL and saves it to the local directory.

Note that you need to add the android.permission.WRITE_EXTERNAL_STORAGE permission to your AndroidManifest.xml file if you want to save the image to external storage.