Native file path - flutter

I have started a development of a music player. I made some additional kotlin logic to retrieve all audio files meta data on device from the Android as a list of meta data. I'm trying to understand how a native path should look like for just_audio to understand. My current implementation uses a deprecated key - MediaStore.Audio.Media.DATA for contentResolver which returns a path like this /storage/emulated/0/Download/scifri20181123-episode.mp3. Can this path be used by the plugin or I need to find another way to retrieve the path in different format?
The same question is valid for the album art.

Yes, if it's a valid file path, then just_audio will be able to open it. You could pass this into setFilePath:
var filePath = '/storage/emulated/....../foo.mp3';
await player.setFilePath(filePath);
This is a convenience method for:
setAudioSource(AudioSource.uri(Uri.file(filePath)));
where Uri.file(filePath) will create a Uri instance containing something like file:///storage/emulated/..... under the hood.
Note that if your app tries to access any file outside of its own app data/cache directories, it also requires permission to do so, and there are other Flutter packages available to help you with this.
Note also that there is also an existing flutter package using the content resolver API called flutter_audio_query, so if it contains what you need, you might not need to write your own Kotlin code.

Related

Store files from flutter application to external storage

Is it always safe to use the path "/storage/emulated/0/{MY APP NAME}" to store the files ?
I Want to store files in the external storage of the android device and I want to store it in a folder named after the app name this folder needs to be located in the external storage.
Have you tried the package path_provider https://pub.dev/packages/path_provider/install?
It comes with the methods getExternalStorageDirectory() and getApplicationDocumentsDirectory() which might be what you are looking for. As far as I know different os types are automatically considered as well.
try the same package of ext_storage named android_external_storage its the same concept putting the downloaded data for example to the download folder.
Check this thread.
The directory an app can access on sdcard looks like /storage/1718-0217/Android/data/com.example.app_name/files. You can get this with the code below.
(await getExternalStorageDirectories())?[1].path;
One app should only access some restricted directories on sdcard. Or you should use Permission.manageExternalStorage.request(), it is strongly restricted and always return false.
Fortunately, per-app directories can also be scanned by other services such like media.

Is it possible to download and execute an uncompiled dart file (Flutter)

I am trying to make an extension based app, where you can download extensions to the app to add features/widgets. Could I somehow run an uncompiled dart file downloaded from a server that stores the .dart files?
download(file.dart)
compile(file.dart)
storeInPersistentDirectory()
if (dartFile.exists) {
ClassFromDownloadedFile.sayHello()
}
The aim is to decrease the app size by storing all of the data related to an extension in the dart file, (classes, json and images in the form of a string...). Users might want to download features, while some don’t need them, in which case they shouldn’t need to download a large app with many features that they will never use, so the initial app size remains small.
Thank you for anyone who knows anything relating to this in advance!
No, it is not possible. You can download a 'resource' such as image/video/mp3 etc. You can even download the dart file but can't compile and execute.
there is a concept in dart and other modern languages called reflection(mirroring).
Reflection allows us to examine and modify the structure and behavior
of a program at runtime.
search about it and check the below links for more information.
dart document
Understanding Reflection and Annotations in Dart
I recommend searching for RMI too.

Flutter: Reading and Writing User Visible Files

I am looking for something along the lines of getExternalStorageDirectory that is available in both iOS and android. The purpose of this directory is to save and retrieve .json configuration files that would be later read back by the application. However the normal getApplicationDocumentsDirectory isn't really what I need. The user is likely to want direct access to the saved files to send them to friends, forums, etc.
Workflows might include:
User A emails a config to User B
User uploads a configuration to a forum
User downloads a configuration from a forum
User creates a configuration offline using a json editor
It seems like I must be missing something obvious here as this seems like a common requirement but I have not found a suitable way to do this.
Thanks
Assuming we're speaking of the path_provider plugin, it actually contains a function called getExternalStorageDirectory, though, as the documentation states, iOS prohibits the user from arbitrarily accessing any directory on the device. I would suggest checking for the platform. If on Android, use getExternalStorageDirectory for Android, and if on iOS, use getApplicationDocumentsDirectory and make it visible to the user. More on that here: How to save a text file in external storage in ios using flutter?
Can't think of any other way of achieving this, since iOS is very strict when it comes to file management (or anything else, really).

How to create APK Expansion Files for assets within flutter

I have created a flutter app with video assets which are about 450mb in size. I have published the app successfully in the Apple appstore. But Google Play does not accept my APK as it is over the limit of 200mb. I tried to go with the approach of creating APK expansion files as the recommended workflow. I have read all available Android documention about expansion files but I still can not figure out how to implement them with flutter.
How do you implement and access assets within APK expansion files in a flutter app?
You're going to have a little bit of fun with this. Basically, until someone implements a plugin to access APK expansion files, you're going to have to write the java code to connect up to Flutter.
It's not prohibitively difficult, it just means that you're going to have to learn about Platform Channels and write a bit of native android code. The documentation probably does a better job of explaining platform channels than I do, but basically the easiest way is to use a MethodChannel to pass data from dart to native and vice-versa.
What you'd do to start is set up a method channel to initiate this process and call it with something like getObbFolder.
On the android side the first thing you need to do is make sure your app actually has the files downloaded. According to the android documentation, you can't guarantee that they will have been so you need to write the logic to download them. I'd recommend using the Download Library they provide as there's all sorts of things to worry about like the device running out of storage, network connectivity, showing progress, etc. I think the documentation for that is moderately straight forward (and if you have issues I'd recommend asking a new question specific to it.
Once you've done that, you need to get the path to the file, and request permission to read it if needed. Some android versions and some devices in other versions (it sounds like it's a bit of a crapshoot to be honest), you need permissions to read the file, while in others you don't. So it's best to just try and ask for permission if trying fails.
To get the directory it's saved in use context.getObbDir()
Then this one way to do it from the android docs:
File obb = new File(obb_filename);
boolean open_failed = false;
try {
BufferedReader br = new BufferedReader(new FileReader(obb));
open_failed = false;
ReadObbFile(br);
} catch (IOException e) {
open_failed = true;
}
if (open_failed) {
// request READ_EXTERNAL_STORAGE permission before reading OBB file
ReadObbFileWithPermission();
}
And for the versions that don't do runtime permission checking add this to your application manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Android doesn't recommend always asking for the permission since sometimes the Obb folder is exempted from needing the READ_EXTERNAL_STORAGE permission.
Now you have two options. One is that you could pass that path back to flutter, and then use flutter's file reading to do something with the data, if the file is something you can directly read. If it's not, you could unpack it either using flutter or using java/kotlin, and then pass back the path to the unpacked files.
If you do choose to unpack the file from android you should do it to one of the directories flutter knows about (with the path_provider plugin for example), or write to wherever you want (and have permission to) and then simply pass the path back to flutter.
Hope that helps!

How to get attachment's (NSItemProvider) file path in Swift 'Share App Extension'?

Note: I am very new to Swift programming (2 days only) and I am working on this piece of code as part of an ElectronJS project. So please don't mind my ignorance regarding the basics of the language. Thanks.
I have created a Swift app containing a Share App Extension.
Requirements:
The Share App Extension should be able to send the absolute file path of the shared files to the container app, i.e. If the user selects a file (abc.txt) from Desktop in Finder and Shares to my Application, then the Share App Extension should be able to get the file path as
/users/userName/Desktop/abc.txt
What I am struggling with here is how to get the file path of the files shared with the Share App Extension. What is the way to get file path of the attachments in NSExtensionItem that is available to the Share App Extension or is it available from some other object ?
(I am able to successfully use App Groups to share data between Share App Extension and the Application)
In the final project, the Share App Extension becomes a part of an ElectronJS project as mentioned earlier.
Is there a standard way to share the aforementioned information (file path of the attachments) from the Share App Extension to the main/renderer processes of the Electron application.
I am sharing the solutions below. Please bear in mind that these might not be the best possible solutions and I am open to suggestions.
Solution to Point #1:
Briefing: The user selects files from Finder to be shared via the Share App Extension of the application which is registered with the OS if the extension context of the selection matches to that of the Share App Extension. Upon doing so, the Share App Extension receives the extension context alongwith NSExtensionItem. The NSExtensionItem object contains the NSItemProvider object which is the object you'd get for all the files (attachments) shared via the Share App Extension.
Now, for each item type that you receive via the Share App Extension, after looking for the data that your function recognizes via hasItemConforminToTypeIdentifier(_:), you can use UTI (Uniform Type Identifier) to identify its data.
Remedy: Here, the crucial part is to understand that one should be treating their input files as firstly being of the type: kUTTypeURL. Then, in the completionHandler for the loadItem method of the NSItemProvider object one would get NSURL which is basically the file path I was looking for.
Solution to Point #2:
Briefing: The Share App Extension has the luxury of being written in Swift but the main app in our project does not ! The main application is written in ElectronJS which is far far far far from being integratable with Swift ! Except for the fact that the application written in ElectronJS has the ability to be packaged in the form of a dmg application, there is very little integratability between ElectronJS and Swift as far as the language and framework intertwining is concerned.
Premise:
So, the premise is to be able to share the filepaths extracted earlier to be passed from the Share App Extension (written in Swift) to the main application (written in ElectronJS). Now, if the main application was a Cocoa application, things would have been much easier. If both of them belong to the same App group, then using the Swift APIs they could have read/written synchronously to the Shared Memory. However, the problem arises as those APIs are not available in ElectronJS. One remedy can be to run the Swift code in a sandboxed environment within the ElectronJS application using nodeJS libraries. However, a sandboxed environment presents its own nuances in data sharing. So, I have kept this approach on hold for now.
So, the approach that I have chosen right now is to use App Data Directory to share this intermediary information. The Share App Extension would be writing the filepath information in the App Data directory of the application and the ElectronJS application would use nodeJs APIs to access this information. Keep in mind that this is a very primitive approach and requires menial efforts but the requirements for this particular case doesn't need stringent security measures anyhow.
However, I am positively looking for a better way to solve Problem #2.