saveImageUrl method

Future<bool> saveImageUrl(
  1. String url,
  2. String fileName,
  3. BuildContext context
)

Implementation

Future<bool> saveImageUrl(
    String url, String fileName, BuildContext context) async {
  Directory? directory;
  try {
    if (Platform.isAndroid) {
      await _requestPermission(Permission.storage);
      await _requestPermission(Permission.accessMediaLocation);

      directory = await getExternalStorageDirectory();
      String newPath = "";
      debugPrint(directory.toString());
      List<String> paths = directory!.path.split("/");
      for (int x = 1; x < paths.length; x++) {
        String folder = paths[x];
        if (folder != "Android") {
          newPath += "/$folder";
        } else {
          break;
        }
      }
      newPath = "$newPath/Image";
      directory = Directory(newPath);
      if (!directory.existsSync()) {
        directory.create(recursive: true);
      }
    } else {
      if (await _requestPermission(Permission.photos)) {
        directory = await getTemporaryDirectory();
        // log(directory.path);
      } else {
        return false;
      }
    }
    File saveFile = File("${directory.path}/$fileName");

    if (await directory.exists()) {
      final dd = await directory.exists();
      // log(dd.toString());
      // SnackBarService().downloadingSnackBar;
      _loadingService.showInfo(
        'Downloading',
      );
      var response = await Dio()
          .get(url, options: Options(responseType: ResponseType.bytes));
      final result =
          await ImageGallerySaver.saveImage(Uint8List.fromList(response.data),
              // quality: 60,
              name: DateTime.now().toString());

      return result['isSuccess'];
    }
    return false;
  } catch (e) {
    // print(e);
    return false;
  }
}