Reputation: 113
I have a question, I am using Path-provider to generate a path for a new generated PDF file from my App… but path provider is storing the file in the App folder & after closing the app the file will be deleted, I am using getExternalStorageDirectory
… how can I save that file to downloads when selected.
Anybody can help me?
just small explanation... the PDF file will be viewed from the App using
flutter_full_pdf_viewer
then from that page I need to let the user select if he wants to save the file or close the hall file.
This is the code for the file save
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);
}
And this is the PDFViewer page code
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,
),
),
);
}
}
============================================================================ after I modified the code now it looks like this for the PDF_save Page (for Methods)
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;
}
}
did I made something wrong??
Upvotes: 0
Views: 2912
Reputation: 332
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;
}
Upvotes: 1