open_file
open_file copied to clipboard
ResultType.fileNotFound
Hi @crazecoder,
Isn't it possible to open files from the assets folder? It isn't accessing anything. I have added their path to the pubsec.yaml file but getting fileNotFound error. I am using an Android phone.
In my pubsec.yaml file: assets: - assets/MezmurGitsawe.pdf - assets/1.jpg
Error message: I/flutter (15303): type=ResultType.fileNotFound message=the assets/1.jpg file is not exists
Thank you
You can just copy the file in the internal memory, then open that file.
Something like this:
static Future<File> getAssetByName(String sourceName) async {
var sampleData = await rootBundle.load("assets/$sourceName");
final path = await _localPath;
var file = File('$path/$sourceName');
file = await file.writeAsBytes(sampleData.buffer.asUint8List());
return file;
}
static Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
//then in your code
var file = await getAssetByName("hello.pdf");
OpenFile.open(file.path, type: "application/pdf");
Thanks a lot for your reply, @matpag. I got this error after trying the code. Do you have any suggestions?
I have imported dart:io'
and path provider libraries. I tried to debug it and saw that the error is thrown on the last line, OpenFile.open(file.path, type: "application/pdf");
. All the other lines worked fine.
Thanks!
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference, null)
You are probably passing a wrong name to the getAssetByName()
method
Remember that if your file is at assets/hello/hello.pdf
you need to call the method in this way: getAssetByName("hello/hello.pdf")
Then remember that you need to add all the folders in the pubspec.yaml
Like:
flutter:
//...
assets:
- assets/
- assets/hello/
Thank you for the quick response. I tried it on an android phone and it is working. I hope it will also work on iOS, right?
Thank you!!