'Flutter open local asset (PDF) in native application
I am trying to bundle my app with a PDF, and let the user open it in the native viewer.
I have tried:
- Copying the data for the PDF to the "Temporary Directory" or the "Document Directory" (from path_provider) and opening from there
- To open I am using 'url_launcher' to open the file. I have tried using both file:
//...urls and just passing the local path ie/.../etc
The files definitely exist, it seems to be a permissions issue that the files in both locations, in both platforms, are in the app's sandbox.
The file definitely exists, and I can open PDFs from web URLs.
is this something flutter can do?
Update 12/8/19
I just got pinged by SO that this question has had a bunch of views and no good answers. For this project I ultimately tried Cordova, Flutter, React Native and eventually gave up and created two native apps to do what I needed to do. They worked OK but the client wanted to make a bunch of UX changes.
So in the end I wrapped the code from the native side of things into a flutter plugin and then did the UX in flutter. Thats the backstory, here is the technical specs of what I hacked together:
For iOS there was an example of using their PDF kit in iOS 11 from github, I did some work there and there was a bunch of manually created features in the repo, so I attached the PDFs I wanted to that project, and wrote a script to present them, then used the flutter bridge to launch.
For Android it was much the same - I copied the files from the app bundle (not flutter assets) to a temp directory and then created a share link and launched the pdf using the native system viewer.
All in all it was a massive stuff around, no fault of Flutter's though, like I said, I used a bunch of multi platform frameworks and none of them would accomplish the job in a satisfactory way. Im sure a better dev could come up with a workable solution though.
Edit: It has been mentioned to use a combination of url_launcher and open_file. They work great for external files but do not work for bundled assets.
Solution 1:[1]
I'm not quite sure if this is still a problem for you. But as I wanted to know the same thing, I tried to figure it out. Here is my result, but I have to mention, that I stored my file in the download folder:
- install url_launcher and simple_permissions
add the following lines to your AndroidManifest.xml and the corresponding iOS equivalent:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />Code for creating pdf:
SimplePermissions.requestPermission(Permission.WriteExternalStorage); String dir = (await getExternalStorageDirectory()).path + "/download"; String filename = "$dir/" + title; File f = new File(filename); f.writeAsBytesSync(data);Code for opening the file
var url = 'file://' + f.path; if (await canLaunch(url)) { await launch(url, forceSafariVC: false, forceWebView: false); } else { throw 'Could not launch $url'; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | T R |
