How to launch system File Manager from Flutter app? - flutter

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

Related

How to open the bottom sheet that contains the list of applications for opening URL or google map. So the user can select their preferred application?

I tried this one. But, it Directly opens on the Google Maps application. The bottom sheet for choosing the preferred application is not showing. How do I achieve it? So, the user can select their preferred application.
This is my code for now,
try {
launchUrl(
Uri.parse("geo:${taskAddress.lat},${taskAddress.lng}"),
mode: LaunchMode.externalApplication,
);
} catch (e) {
} finally {
launchUrl(Uri.parse(
"google.navigation:q=${taskAddress.lat},${taskAddress.lng}&mode=d"));
}
I think you can't do that. It's user device preferred setting. If the user set it to "Always Open" or similar it will not ask again.

Unity does not open the Edge Browser on a Hololens 2 when providing a pdf file path

I'm trying to open a PDF within a Unity app deployed to a Microsoft Hololens 2 using the MRTK.
I want to use the integrated Edge Browser, that is able to load within the app and to run simulatenously.
Edge by itself is also able to load pdf files.
I expected using LaunchUri would open the file from a provided location
public void Launch()
{
var absolutePath = Path.Combine(_myDocuments, _subfolder);
absolutePath = Path.Combine(absolutePath, _fileName);
#if UNITY_WSA
UnityEngine.WSA.Launcher.LaunchUri(absolutePath, true);
#else
Application.OpenURL(absolutePath);
#endif
}
Launching the Edge Browser with a website as the string, works. Entering the file path manually into the browser window to load the pdf also works, but launching the Browser by directly providing the full path to my file, does not open any browser window.
I suspect, that it doesn't work because Edge is not setup to be the default app for PDF files, but when using the button within Edge to make it the default app, it just tells me that it failed and I can't find any other way to achieve that.
Does anyone else have an idea how I would be able to load Edge from within an Unity app and providing the path to my pdf file so it automatically loads on a button press?
The PDF is stored on the local file system under "Documents".
As mentioned, the file is accessible and can be opened by Edge. Using the system File Browser to manually open the PDF from within the system automatically uses Edge and opens the file as expected, which is the behaviour I would like to copy from within Unity.
Environment
Unity 2020.3.15f
Hololens 2 latest Update
Win10 SDK target 10.0.19041.0
Build to ARM64 for Hololens
edit:
I ended up using
UnityEngine.WSA.Launcher.LaunchFile(folderType, relativePath, false);
You can try Launcher.LaunchFileAsync to launch a PDF file with Edge. This method required a StorageFile as the parameter, which can be got by FileOpenPicker or GetFileFromPathAsync.
Please refer to the following code.
private async void PickAndLaunchFile()
{
// First, get a file via the picker.
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Next, launch the file.
bool success = await Launcher.LaunchFileAsync(file);
}

Flutter folder picker

I have a button on screen, when you click button I need to open some folder picker.
I have searched for package, but I didn't found any package that also support Android and iOS.
Does someone knows some package who will resolve this problem, or something I could use to make solution of picking folder on iOS?
EDIT:
I found way to get directory picker functionality,I didn't test on iOS but in documentation says it works on both (Android and iOS).
String path = await FilePicker.platform.getDirectoryPath();
With this line above you get new screen to select your directory and get a path like result from that Future.
This is solved using file_picker package.
Use the package file_picker.
Easy to use:
FilePickerResult? result = await FilePicker.platform.pickFiles();
if(result != null) {
File file = File(result.files.single.path);
} else {
// Do something else...
}

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