Is there any folder that can be accessed through a Xamarin.Forms app? The app will be deployed to Android, iOS and as a UWP app. I have a database file and so far, I am using SpecialFolders.Personal, which works for Android and iOS, but not UWP. I would need to get permission for this folder in Windows.
So is there any folder I can use across all 3 devices for which I don't need any special permissions?
Xamarin.Forms has provided Environment.SpecialFolder.LocalApplicationData enum that use to get universal storage path. And it need to be combined with Environment.GetFolderPath method.
var ApplicationDataPath = Environment.GetFolderPath(SpecialFolder.LocalApplicationData);
For more please refer File Handling in Xamarin.Forms
Related
I am running into an issue in a Xamarin Forms project I am working on, building and using on UWP and Android (don't care about iOS currently). My app is one which connects to a Bluetooth device to record data, stores it within the app, and exports the data into a .txt file or similar that is used for other purposes outside the app. On Android, targeting either Android 11 or Android 12.
I am working with Xamarin 17.4.0.301, Visual Studio 17.4.0, and .NET Framework 4.8.04084, and Xamarin.Android SDK 13.1.0.1.
My issue is trying to save to a file repeatedly that is accessible. Currently, I use a file hidden in the app's internal storage, using a filepath like this:
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "data.txt");
This of course works on both UWP and Android; however, since it is internal, it cannot be accessed by a file explorer on Android. To be seen on Android, I have to export it from the app into a file explorer app, using code like this, where the command is tied to a button press:
OnShareDataCommand = new Command(ShareDataButtonClick);
...
private async void ShareDataButtonClick() {
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "data.txt");
await Share.RequestAsync(new ShareFileRequest {
Title = "Share Data",
File = new ShareFile(fileName)
});
}
This requires a user to press the button to export. My desire is for the system to automatically make a file somewhere accessible in the file explorer (like a Downloads folder) and continuously save to it when the function is called. To do this, I have enable access to external storage as such in the Android Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
But I am running into issues accessing it. The first is when I try to create a filepath that is in external storage like this, based on this StackOverflow question:
var fullPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "file_name.txt");
When I do this, it says Android is not in the context. I imagine this is because I am in Xamarin Forms building for multiple platforms. How can I fix this? How can I access Android's External Storage while in Xamarin Forms? Can I make a platform specific function, and if so, how do I go about it?
This Xamarin forums solution did not work.
I have tried using this line from the External Storage for Xamarin.Android guide, which also does not work because it says Environment does not have the method used:
Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDocuments).AbsolutePath
Any advice? I do not want to make it an Android specific app, as I need the UWP functionality, but I could make a platform-specific save function.
Since Android 11 Read/Write External storage is not allowed, the permission will not be granted. Unless you have an app like a file explorer and even then you have to apply to Google before it will be accepted. So you need to forget about writing to external storage.
See the following links
https://medium.com/swlh/sample-for-android-storage-access-framework-aka-scoped-storage-for-basic-use-cases-3ee4fee404fc
https://www.zoftino.com/how-to-create-browse-files-option-in-android
https://mateuszteteruk.pl/how-to-use-android-storage-access-framework-with-example
https://thedroidlady.com/2020-08-24-android-scoped-storage-demystified
I did it with my app back when I released for Android 11.
If you need any further help just reply here. I should add, I only write Android apps, so I can't help out with anything Forms related or UWP
#ToolmakeSteve, Is your question addressed to me? I'm writing in C# and Xamarin.Android. I don't think a couple of code snippets would provide anything useful. Just follow the links and the link you posted. I'll attempt to provide a test app, but I've not enough time to do that at the moment.
I'm beginner programmer in general.
Is it possible to add files to Android app folders in /data/data using my app on a non-rooted device? I'm planning to make an app using C++ and I’m not using Android Studio IDE, my app should add whatsapp stickers to whatsapp.
No, for security reasons. You can put files in your app's portion of internal storage, but you cannot put files into other apps' portions of internal storage.
I have an Android App I downloaded and installed, I have also extracted the APK,
Is there some way from the apk file to tell what programming language was used to write the program?
Are there any tools are available to help with this?
What languages can be determined and which ones cannot?
Normally most android app is build using the following texhnologies:---
React Native app
Cordova app
Native java/kotlin app
To determine the technology, you need to decompile the app first using this link
Then look for "assets" folder. Normally, asset folder should be present at the very top of application hierarchy.
Then you can determine the app technology by the following prediction:--
If asset folder is present and there have any file with the extension ".bundle"(mainly the file name is "index.android.bundle") then the app is build using React Native.
If the assets folder contain a folder name "www" and "assets/www" contain the following file/folder structure then it is a cordova app.
If neighter 1 or 2 is true then the app may be written using native code, i.e using java/kotlin
You can do a reverse engineering : (https://www.apkdecompilers.com/ or http://www.javadecompilers.com/apk) decompile the apk and see the code and its programming language.
Not an accurate way to get the specific technology, but through adb, you could check the activity name of an app.
Usually when you open different activities or pages, with native technology the ActivityName will be different like below (Playstore app):
whereas in hybrid apps such as Ionic e.g, it is usually bundled up into one, hence the only activity will be MainActivity... like the Hamilton app made with flutter:
Am making an android application using react-native. The application is supposed to have its own separate folder in the phone to help save all the books and content that the user bought from the application. i also need to hide the folder or kind of encrypt it so the user doesn't share his own books or content he bought with any other person.
your application folder already define at context.getFilesDir().getPath() path application install time and one more thing this folder is hide default. only your application is access
These are the two most popular file system management and download libraries for react-native:
react-native-fetch-blob
react-native-fs
I use react-native-fetch-blob because it has better support for opening downloaded files and download manager for android. There is also friendly dir API with easy access to commonly used folders.
Does android support cross application file access i mean Lets say Application A created a file called
"abcd.txt", can we access the file "abcd.txt" from another application B like how it can be possible in windows and not possible in iOS. please help.
This is possible, even multiple manners.
If both applications are from the same developer they could use the same shared user id. This is an attribute in the AndroidManifest.xml, android:sharedUserId. When different applications/apks have the same userid they are able to read each others files. When using this technique, files are still prive to the rest of the OS, but are readable/writable by other apks from the same developer.
Another posibility is using the sdcard to share data. That storage is publicly available.
Android has built-in support for sharing data with other applications through the use of ContentResolvers. This does not work for files, but is probably the preferred way to shared data between applications.