image_gallery_saver ios获取下载路径

答案是获取不到的需要再加一个插件去适配ios

 path_provider: ^2.0.1

可以先从pub.dev去看下集成文档 有些配置需要配置上去

 

配置好后代码如下


  Future<void> _save(String url, {bool share = false}) async {
    //取文件后缀
    String localPath = url.substring(0, url.indexOf("\?"));
    //edcd6694de4555bda0ce1cf3ddd7c9ed.jpg
    localPath = localPath.substring(url.lastIndexOf("/") + 1);
    var response = await Dio()
        .get(url, options: Options(responseType: ResponseType.bytes));
    var android_result = null;
    File iosPath = null;
    var imageData = Uint8List.fromList(response.data);
    EasyLoading.show(status: '下载中,请稍后...', dismissOnTap: true);
    if (Platform.isAndroid) {
      // 如果是Android 的话,直接使用image_gallery_saver就可以了
      // 图片byte数据转化unit8

      // 调用image_gallery_saver的saveImages方法,返回值就是图片保存后的路径
      android_result = await ImageGallerySaver.saveImage(imageData,name:localPath.substring(0,localPath.indexOf(".")-1) );
      // 需要去除掉file://开头。生成要使用的file

    } else if (Platform.isIOS) {
      // 获取手机存储
      Directory applicationDir = await getApplicationDocumentsDirectory();
      // 判断路径是否存在
      bool isDirExist = await Directory(applicationDir.path).exists();
      if (!isDirExist) Directory(applicationDir.path).create();
      // 直接保存,返回的就是保存后的文件


      iosPath =
          await File(applicationDir.path + localPath).writeAsBytes(imageData);
    }
    //判断是否下载成功  下载成功可以执行分享逻辑
    if (Platform.isAndroid) {
      bool status = android_result['isSuccess'];
      if (status) {
        String filePath = android_result['filePath'];
        if (filePath != null) {
          filePath = filePath.replaceAll("file://", "");
        }
        SpUtil.putString(_downCacheKey, filePath);
        setState(() {
          _isDownloadPic = true;
          EasyLoading.showSuccess("下载成功");

          if (share) {
            Share.shareFiles([filePath],
                text: widget.avatarEmoticonsListsData.name);
          }
        });
      } else {
        String errorMessage = android_result['errorMessage'];
        EasyLoading.showError("下载失败,原因:$errorMessage");
      }
    } else if (Platform.isIOS) {
      if (iosPath != null) {
        if (iosPath.existsSync()) {
          SpUtil.putString(_downCacheKey, iosPath.path);
          setState(() {
            _isDownloadPic = true;
            EasyLoading.showSuccess("下载成功");
            if (share) {
              Share.shareFiles([iosPath.path],
                  text: widget.avatarEmoticonsListsData.name);
            }
          });
        }
      } else {
        EasyLoading.showError("保存失败。请授予保存照片权限");
      }
    }
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值