【问题标题】:save a temp file to download file Android/IOS in flutter保存临时文件以在颤动中下载文件 Android/IOS
【发布时间】:2021-08-21 04:03:38
【问题描述】:

我有一个问题,我正在使用 Path-provider 为我的应用程序中新生成的 PDF 文件生成路径……但路径提供程序将文件存储在 App 文件夹中,并且在关闭应用程序后,文件将被删除,我正在使用getExternalStorageDirectory...如何在选择时将该文件保存到下载。 任何人都可以帮助我吗? 只是一个小解释...... PDF文件将使用从应用程序查看 flutter_full_pdf_viewer 然后从那个页面我需要让用户选择他是要保存文件还是关闭大厅文件。 这是文件保存的代码

  try {
    final dir = await getExternalStorageDirectory();
    String documentPath = dir.path;
    fullPath = "$documentPath/$fileName";
    final file = File('$fullPath');
    await file.writeAsBytes(await pdf.save());
    print('XXXXXXXXX$fileName');
    return file;
  } catch (e) {
    // print('we have a problem');
  }
  print(fullPath);
}

这是 PDFViewer 页面代码

  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: PDFViewerScaffold(
          appBar: AppBar(
            title: Text('PDF page'),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.share),
                onPressed: () {
                  Share.shareFiles(
                    ['$path'],
                  );
                  print('iiiiiiiiiiiiii$path');
                },
              ),
              IconButton(
                icon: Icon(Icons.save),
                onPressed: () {},
              ),
            ],
          ),
          path: path,
        ),
      ),
    );
  }
}

================================================ =============================== 在我修改代码后,PDF_save 页面(用于方法)看起来像这样

Future<bool> saveFolder(String fileName) async {
  Directory directory;
  try {
    if (Platform.isAndroid) {
      if (await _requestPermission(Permission.storage)) {
        directory = await getExternalStorageDirectory();
        String newPath = "";
        print('ZZZZZZZZzzzzZZZZz$directory');
        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 + "/SAMApp";
        directory = Directory(newPath);
        print('AAAAAAAAAAAAAAAA$newPath');
        print('AAAAAAAAAAAAAAAA$fileName');
      } else {
        return false;
      }
    } else {
      if (await _requestPermission(Permission.photos)) {
        directory = await getTemporaryDirectory();
      } else {
        return false;
      }
    }
    File saveFile = File(directory.path + "/$fileName");
    if (!await directory.exists()) {
      await directory.create(recursive: true);
    }
    if (await directory.exists()) {
      // await dio.download(saveFile.path, onReceiveProgress: (value1, value2) {
      //   setState(() {
      //     progress = value1 / value2;
      //   });
      // });
      // if (Platform.isIOS) {
      //   await ImageGallerySaver.saveFile(saveFile.path,
      //       isReturnPathOfIOS: true);
      // }
      File savedFile = File(directory.path + "/$fileName");
      var savedPath = directory.path + "/$fileName";
      print('$savedPath');
      print('$savedFile');
      savedFile.writeAsBytesSync(byteList);
      if (savedPath != null) {
        print('NO saved Path');
        // await Navigator.pushReplacement(
        //   context,
        //   MaterialPageRoute(
        //       builder: (context) => SharePage(
        //             imageFilebytes: pngBytes,
        //             imagePath: savedPath,
        //           )),
        // );
      } else {
        print("waiting for savedpath");
      }
      return true;
    }
    return false;
  } catch (e) {
    print(e);
    return false;
  }
}

我是不是做错了什么??

【问题讨论】:

    标签: android ios flutter flutter-packages


    【解决方案1】:
    This is worked to save the image. It can be useful for you.
    
    Future<bool> saveFile(var byteList, BuildContext context) async {
    Directory storageDir;
    try {
    if (await requestPermission(Permission.storage)) {
    storageDir = await getExternalStorageDirectory();
    
    String newPath = '';
    List<String> folders = storageDir.path.split('/');
    for (int x = 1; x < folders.length; x++) {
    String folder = folders[x];
    if (folder != 'Android') {
    newPath += '/' + folder;
    } else {
    break;
    }
    }
    newPath = newPath + '/xyz';
    storageDir = Directory(newPath);
    } else {
    if (await requestPermission(Permission.photos)) {
    storageDir = await getTemporaryDirectory();
    } else {
    return false;
            }
          }
          if (!await storageDir.exists()) {
            await storageDir.create(recursive: true);
          }
    if (await storageDir.exists()) {
            //List<int> bytesSync = widget.pickedImage.readAsBytesSync();
            DateTime date = DateTime.now();
            String baseFileName = 'abc' + date.toString() + ".jpg";
            File savedFile = File(storageDir.path + "/$baseFileName");
            savedPath = storageDir.path + "/$baseFileName";
            savedFile.writeAsBytesSync(byteList);
            if (savedPath != null) {
              await Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      builder: (context) => SharePage(
                            imageFilebytes: pngBytes,
                            imagePath: savedPath,
                          )));
            } else {
              print("waiting for savedpath");
            }
            return true;
          }
        } catch (e) {
          print(e);
        }
        return false;
      }
    

    【讨论】:

    • 嗨,感谢您的回复,我尽可能多地尝试实现相同的功能...但现在我遇到了另一个问题...我在我想要的位置创建了文件夹...也文件已经保存在新文件夹中了……但主要问题是文件为0Bytes,无法打开……不知道你能不能查看我的代码
    • 我觉得保存文件的时候有些问题的意思可能是保存文件的方法有些问题。我添加了这个链接,也许它对你有用。尝试更改保存方法。 stackoverflow.com/questions/61645917/…
    猜你喜欢
    • 2021-10-15
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多