我們在安卓里面偎巢,下載文件以后,打開文件夾就可以看到兼耀,但是蘋果手機上面打開文件夾,看不到應用求冷,
想要在蘋果手機上顯示應用的文件夾瘤运,那要怎么做呢?
1匠题,dio下載文件保存到本地
ios設置文件夾是 getTemporaryDirectory()拯坟,不要使用getApplicationDocumentsDirectory()看不到
getTemporaryDirectory()默認目錄是Library/Caches,這個目錄沒有暴露
暴露的目錄是Documents
將Library/Caches替換為Documents
dirloc= dirloc.replaceFirst("Library/Caches", "Documents/");
Future<void> downloadSaveFile(EmailContent emailContent, int index) async {
? ? if (ObjectUtil.isNotEmpty(emailContent.attachments[index].download) &&
? ? ? ? emailContent.attachments[index].download) {
? ? ? FilePickerResult result =
? ? ? ? ? await FilePicker.platform.pickFiles(allowMultiple: false);
? ? ? return;
? ? }
? ? final FileSystem fs = MemoryFileSystem();
? ? Map<String, dynamic> user = await SharedPrefercnes.getString('email_user');
? ? userInfo = EmailUser.fromJson(user);
? ? Attachments attachments = emailContent.attachments[index];
? ? String downloadFile =
? ? ? ? '${Api.download_file}/?raw_name=${widget.mailBoxType}&uid=${widget.uid}&sid=${attachments.sid}';
? ? Map<String, dynamic> headers = {
? ? ? "Authorization": "JWT ${userInfo.data.emailToken}",
? ? ? 'Accept': 'application/json, text/plain, */*',
? ? };
? ? String url = Api.ip + downloadFile;
? ? Dio dio = Dio();
? ? PermissionStatus status = await PermissionHandler()
? ? ? ? .checkPermissionStatus(PermissionGroup.storage);
? ? //判斷如果還沒擁有讀寫權限就申請獲取權限
? ? if (status != PermissionStatus.granted) {
? ? ? var map = await PermissionHandler()
? ? ? ? ? .requestPermissions([PermissionGroup.storage]).then((value) {
? ? ? ? print(value);
? ? ? });
? ? ? if (map[PermissionGroup.storage] == PermissionStatus.granted) {
? ? ? ? status = PermissionStatus.granted;
? ? ? }
? ? }
? ? print(attachments.name);
? ? if (true) {
? ? ? String dirloc = "";
? ? ? if (Platform.isAndroid) {
? ? ? ? dirloc = "/sdcard/download/";
? ? ? } else {
? ? ? ? dirloc = (await getTemporaryDirectory()).path;
? ? ? dirloc= dirloc.replaceFirst("Library/Caches", "Documents/");
? ? ? }
? ? ? var randid = random.nextInt(10000);
? ? ? try {
? ? ? ? //2韭山、創(chuàng)建文件
? ? ? ? FileUtils.mkdir([dirloc]);
? ? ? ? String token = "JWT ${userInfo.data.emailToken}";
? ? ? ? var options = Options(headers: <String, String>{
? ? ? ? ? "Authorization": token,
? ? ? ? });
? ? ? ? print(url);
? ? ? ? //3郁季、使用 dio 下載文件
? ? ? ? await dio.download(url, dirloc + attachments.name, options: options,
? ? ? ? ? ? onReceiveProgress: (receivedBytes, totalBytes) {
? ? ? ? ? setState(() {
? ? ? ? ? ? downloading = true;
? ? ? ? ? ? //4、連接資源成功開始下載后更新狀態(tài)
? ? ? ? ? ? progress = (receivedBytes / totalBytes);
? ? ? ? ? });
? ? ? ? });
? ? ? } catch (e) {
? ? ? ? print(e);
? ? ? }
? ? ? // setState(() async {
? ? ? downloading = false;
? ? ? progress = 0;
? ? ? path = dirloc + attachments.name;
? ? ? print(path);
? ? ? emailContent.attachments[index].download = true;
? ? ? ToastUtils.getInstance().showToast("下載完成,請在文件夾中查看");
? ? ? // });
? ? } else {
? ? ? setState(() {
? ? ? ? progress = 0;
? ? ? ? _onPressed = () {
? ? ? ? ? downloadSaveFile(emailContent, index);
? ? ? ? };
? ? ? });
? ? }
? }
2钱磅,共享應用目錄到蘋果手機文件夾梦裂,
在info.list 添加Supports Document Browser ,
Supports opening documents in place盖淡,
UIFileSharingEnabled年柠,
設置為true。
大功告成褪迟,打開蘋果文件夾冗恨,就可以看到本應用的目錄,打開目錄就看到應用下載的文件啦味赃!