Open PDF In PDF Viewer - flutter

I have a PDF in the assets folder: assets/my_resume.pdf.
How can I open it in a PDF viewer? I don't want to open it in a widget. I want it to open outside of my app with whatever PDF viewer is available to this device.
I tried: https://pub.dev/packages/open_file but that doesn't work with assets.
Future<void> launchPdf() async {
}
How can I do this? I mostly care about mobile so not even considering web.

I don't know if this is the best method, but you could load the pdf file data, and save it to, for example, the Document directory on the device, and after that, use the open file package to open the pdf with a native device app.
void openAsset() async {
// load pdf into ram
ByteData pdf = await DefaultAssetBundle.of(context).load('assets/your_pdf.pdf');
// get documents directory
Directory documents = await getApplicationDocumentsDirectory();
// write data to a file in the documents directory
await File(documents.path + '/your_pdf.pdf').writeAsBytes(pdf.buffer.asUint8List());
// call open_pdf function
// ...
}
(Didn't test the code)

Related

How to launch system File Manager from Flutter app?

From my flutter I want to launch the default File Manager. I don't want to select file from it for my app. I just want to launch it (preferably showing the directory that I want it to show) so that the user can pick a file from that directory and do whatever they want with it.
Can anyone please tell me how to do it? I see a lot of code and examples for picking a file but that's not what I need. Just want to launch whatever is a default file manager.
Please do not suggest FilePicker. That is not what I am looking for. I do not want to "pick" a file. I know how to pick a file. I just want the default file manager app to be launched. I don't want the control to be returned back to the app irrespective of whether the user selects a file or not.
This was causing me grief too. Here's my solution.
Using 'external_app_launcher' you are able to launch apps based on their URL scheme, so we have...
import 'package:external_app_launcher/external_app_launcher.dart';
okButton = TextButton(
child: const Text("Open Files"),
onPressed: () async {
await LaunchApp.openApp(iosUrlScheme: 'shareddocuments://', openStore: false);
},
);
This will launch the app, I'm still working my solution as I want to open my App's folder so will update as I get more.
You can use the file_picker package.
Usage
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path);
} else {
// User canceled the picker
}
Edit: Now I understand what you want. Check https://pub.dev/packages/external_app_launcher

How to share files with a different extension

As I said in the title, I'm trying to share files converting an image file to PNG format. I have tried to use the packages share_plus and social_share. I have this code:
img.Image? image = img.decodeImage(File(imageXFile.path).readAsBytesSync());
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(img.encodePng(image));
setState(() {
_items.add(new Item(fileName, imageFile));
});
I'm trying to share the file using imageFile.path. I also tried including the image/png as mimeType, but still get the original file name and extension when I share the file.
UPDATE: I tried to find a solution for this bug, but nothing yet. It appears that is not a share_plus issue, image package neither. I printed the uri and mimeType in share_plus Kotlin code before start the activity and I got the right infos (png mimetype and the .png extension). I don't know how to proceed now.
My last (and unsuccessful) test was this:
final XFile? imageXFile = await _picker.pickImage(source: ImageSource.gallery);
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(await imageXFile.readAsBytes());
Share.shareFiles([imageFile.path], mimeTypes: ["image/png"]);
I tried to pick a PNG image, save it and share it immediately, but still share the image as JPG.
OBS: While I was writing this update, I noticed that when I share with Telegram (as uncompressed file), it will be sent as JPG. When I share with Outlook, it is attached as PNG (with the name and extension that I want to).

how to upload image to firebase storage from assets under folder. Flutter

I want to upload an image from my assets folder into my firebase storage under a folder with the user id as the folder name. but is the first time on my life using firebase storage so I have no idea how to do that. I saw people only upload those images as "File" variable from the image picker, but I have no clew how to convert the image to File variable. Thank you.
I found that line of code:
FirebaseStorage.instance.ref(uid).child("profile-pic.png").putFile(/*what am i puting here?*/);
To get the files from the assets folder, then you must use the rootBundle, as in:
var fileName = "assets/demo.txt";
ByteData bytes = await rootBundle.load(fileName); //load sound from assets
Uint8List rawData = bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
FirebaseStorage.instance.ref('$fileName').putData(rawData);
Try that.

Can't remove PDF file from cache Flutter

I'm using the following library to display a pdf in my application "https://pub.dev/packages/advance_pdf_viewer".
I'm displaying the pdf from file assets like this:
loadDocument() async {
document = await PDFDocument.fromAsset('assets/res/sample.pdf');
setState(() {
_isLoading = false;
});
}
The problem is that I want to replace the file with a new version, but it keeps showing the old one.
What I tried:
1- Changing file name
2- Flutter clean
3- Pub get
4- Deleting the whole file from the project, and still showing ( which means it's somewhere in the cache )
5- Deleting the app
All previous solutions didn't work, and it's driving me crazy.
Thanks

Flutter - issue with downloading the file and opening it with the default app on ios

I have built an app with flutter, what I am trying to do is download a document, these are jpeg, jpg or pdf files, from my REST api, and open it within the app so the user can see it,
The below piece of code is the issue, I am trying to use the openfile package in flutter, the code works perfectly on android, it opens the image automatically, but on ios, it gives the attached screen after attempting to download.
I am putting part of my code which I believe is the issue.
Code to specify the plugin parameter
if (Platform.isIOS)
directory = await getTemporaryDirectory();
else
directory = await getExternalStorageDirectory();
Code to open the file
OpenFile.open(downloadPath).then((_result) {
_closePopup();
print(_result);
}).catchError((error) {
print("Could not open file *** $error");
FileDownloader.lastError =
"Could not open file: " + error.toString();
_closePopup();
});
} else {
Any guidance on this will help.
Buttton sample I am trying to work out
IOS Behaviour with code