问题
In a Flutter project I create a pdf document. I can save the document in the path of the app. But the user has no access to it. Alternatively, how can I save the file to another folder where the user sees it?
import 'package:path_provider/path_provider.dart';
Future<void> savePdfDocument() async {
final PdfCreater generatedPdf = PdfCreater(document);
final List<int> generatedPdfDocument = generatedPdf.buildPdf();
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/example.pdf';
final File file = File(path);
file.writeAsBytesSync(generatedPdfDocument);
}
回答1:
if you are using this app only on Android, you can try using:
Future<Directory> getExternalStorageDirectory()
from the path_provider
plugin.
This method is used to get to the top and publically accessible directory in the storage.
From here you can add subdirectory path as /Downloads/your_file_name
.
This should probably help you. If it doesn't, add a comment and let me know.
The method's source code documentation:
/// Path to a directory where the application may access top level storage.
/// The current operating system should be determined before issuing this
/// function call, as this functionality is only available on Android.
///
/// On iOS, this function throws an [UnsupportedError] as it is not possible
/// to access outside the app's sandbox.
///
/// On Android this uses the `getExternalFilesDir(null)`.
回答2:
Use downloads_path_provider, on android it will save the file on Downloads.
for IOS, you need to use getApplicationDocumentsDirectory
from path_provider and add permissions on info.plist
to make the folder visible to the user:
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
来源:https://stackoverflow.com/questions/59154031/flutter-save-file-visible-to-the-user