简体   繁体   English

无法在颤振中写入文件

[英]Can't write to file in flutter

Here is the code for making and writing to the file, I am able to write to temp files but anything else doesn't seem to work for some reason.这是制作和写入文件的代码,我可以写入临时文件,但由于某种原因,其他任何东西似乎都不起作用。

Directory? dir;
await Permission.storage.request();
if (!await Permission.storage.isGranted) return;
String? dirPath = await FilePicker.platform.getDirectoryPath();
if (dirPath == null) return;
dir = await Directory(dirPath).create(recursive: true);
for (var pdf in selected) {
  var bytes = base64Decode(pdf.pdfData.replaceAll('\n', ''));
  File savedFile = await File(dir.path +
      '/Receipt-' +
      DateTime.now().millisecondsSinceEpoch.toString() +
      '.pdf')
    .create(recursive: true);
  savedFile.writeAsBytes(bytes.buffer.asUint8List());
}

Here is the full error这是完整的错误

E/flutter (21592): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot create file, path = '/storage/emulated/0/Receipts/Receipt-1634356377430.pdf' (OS Error: Operation not permitted, errno = 1)
E/flutter (21592): #0      _File.create.<anonymous closure> (dart:io/file_impl.dart:255:9)
E/flutter (21592): #1      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (21592): #2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (21592): <asynchronous suspension>
E/flutter (21592): #3      PocketLoaded.downloadPDFs (package:receipts/cubit/pocket_state.dart:54:24)
E/flutter (21592): <asynchronous suspension>

I also have this in my manifest inside the manifest tag我的清单中的清单标签中也有这个

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If I'm doing this wrong how should I be writing to a file that the user can access outside of the app?如果我做错了,我应该如何写入用户可以在应用程序外部访问的文件?

Things have changed after android 10. You need to add this permission in your AndroidManifest.xml . android 10 之后情况发生了变化。您需要在AndroidManifest.xml添加此权限。

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

To get the permission use获得许可使用

if (await Permission.manageExternalStorage.request().isGranted) {...}

NOTE: This permission is considered pretty risky so play store will reject your app unless it's your app's core functionality.注意:此权限被认为是非常危险的,因此除非它是您的应用程序的核心功能,否则 Play 商店将拒绝您的应用程序。

This is what google says这就是谷歌所说的

Core functionality is defined as the main purpose of the app.核心功能被定义为应用程序的主要目的。 Without this core functionality, the app is "broken" or rendered unusable.如果没有这个核心功能,应用程序就会“损坏”或无法使用。 The core functionality, as well as any core features that comprise this core functionality, must all be prominently documented and promoted in the app's description.核心功能以及包含此核心功能的任何核心功能都必须在应用程序的说明中进行显着记录和宣传。

And if you are planning to create a file in an external directory and you are not able to find it, it's because you'd have to tell the device to refresh for the files.如果您打算在外部目录中创建一个文件,但找不到它,那是因为您必须告诉设备刷新文件。 So I'd recommend using this package and passing your newly created file path to it or if you wanna do it manually with kotlin here's the code所以我建议使用这个并将你新创建的文件路径传递给它,或者如果你想用 kotlin 手动完成这里的代码

    private fun broadcastFileUpdate(path: String) {
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(File(path))))
        println("updated!")
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM