I am generating a file in a .NET MAUI application, and I want to save it to the Files folder. I followed the issue asking about how to save a temporary file in .NET MAUI, but I want my output to be "permanent" location (well, until the user deletes it, anyway).
I am using the following code:
string FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myfile.gz");
byte[] arr = GetDataToSave();
await File.WriteAllBytesAsync(fileName, arr);
I'm running this code on an Android emulator, and I'm expecting to see a file created here:
I'm looking for the file so I can drag it to my Windows machine and inspect the contents to make sure I created the file successfully. But, it's not showing up.
How can I save a file from .NET Maui in such a way that I can see it in Files on the Android Emulator?
From the following code , we can find that you are trying to save file to internal storage.
string FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myfile.gz");
And from document File Storage and Access with Xamarin.Android, we know that
Internal Storage – this is a portion of the file system that can be
accessed only by the application or the operating system.
So, if you save file to internal storage, you cannot access this file by other apps. That's why you can't see this file with other apps.
Of course, if you want to see this file with other app, you can save your file to External Storage.
External Storage is a partition for the storage of files that is accessible by all apps, the user, and possibly other devices. On some devices, external storage may be removable (such as an SD card).
For more details, you can check document: External storage.
And there is also an sample included in above document, you can check it here: LocalFiles.
Related
It seems that getExternalStoragePublicDirectory() is now deprecated, so I need an alternative.
Say I have file the foo.png in my app's internal storage. I can easily access this file with
val foo = File(context.filesDir, "foo.png")
My problem is that I want to move this file to the user's Downloads directory, so that they can see it on their phone.
This answer uses DownloadManager. Unfortunately this is an image I generate myself, not download from somewhere.
You have two options.
Use Storage Acces Framework to let the user choose the Download directory .
Use MediaStore to insert file to Download directory.
The MediaPlugin library creates files in
storage/emulated/0/Android/data/[app_name]/files/Pictures/18-Feb-19/1550503112_in.jpg
What would I use to get access to the picture in this path later?
I'm trying to avoid using a hard-coded string... I've tried googling it but I'm getting really confused as to how to get this path by using Android predefined values like android.os.environment.datadirectory or System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures); etc.
Android groups the filesystem into two different types of storage:
1 Internal Storage – this is a portion of the file system that can be accessed only by the application or the operating system.
You can access the internal storage using code like this
System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures)
And the path is like this:
/data/user/0/MyApp.Android/files or /data/data/{package name}/files
2 External Storage – this is a partition for the storage of files that is accessible by all apps, the user, and possibly other devices. On some devices, external storage may be removable (such as an SD card).
You can access the external storage using code like this
Android.Content.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures)
And the path is like this
/storage/emulated/0/Android/data/{package name}/files
So you can see the path Media Plugin has created is the external storage path. You can access it using
Android.Content.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures)
I am working on a mobile app, using Qt/C++, right now focusing on Android.
My app needs to store some permanent data, in a private and secure way (not accessible to other apps, protected as much as possible):
some basic key/value settings: QSettings seems to be what I need here. The question being where does this end up in Android, is it stored in the shared preferences section?
binary files, such as a few pics (these are created by the app, not static resources). I would have stored this in an internal storage file; where would I store this in Qt? Do I use Qt's file capabilities, and java calls to find my app's internal storage folder, or is there a Qt object designed for that?
Thanks.
Android maintains a standard storage for applications under the path /data/user/0 , where each application gets storage space. so if you have an application named org.qtproject.example.myApp, Android automatically creates storage space for this app:
/data/user/0/org.qtproject.example.myApp
The settings are stored under the files folder of this path, as ../files/.config/OrganizationName/AppName.conf
When you want to store information in Android you don't use absolute paths, instead you specify the location of your storage using Qt QStandardPaths which usually returns location under the application path mentioned above, so for example to store a file mySomeFile, you would set the path using QStandardPaths like:
auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
auto fileName= path + "/mySomeFile";
and the file is stored as :
/data/user/0/org.qtproject.example.myApp/files/mySomeFile
I am using PhoneGap with File plugin. I am able to read & write files to file system and those are persisted correctly. But when I try to locate those files from native file explorer app on tablet or from Windows Explorer (through USB connection), I can't find any traces of these files by browsing to folder that PhoneGap claims to store those (Tablet\Android\data\com.xxx.yyy\files) to nor with search functionality. I do not have any external cards attached to my Android tablet. Any ideas how to find those files?
After carefully studying Android documentation about storage options I spotted that this is the intended behavior of internal storage which is used for applications private data that shouldn't be possible to read by other applications accessing the file system.
I have all the time thought that the external referred to SD cards but instead the external storage is described like this
Every Android-compatible device supports a shared "external storage"
that you can use to save files. This can be a removable storage media
(such as an SD card) or an internal (non-removable) storage. Files
saved to the external storage are world-readable and can be modified
by the user when they enable USB mass storage to transfer files on a
computer.
which seems to be the exact behavior I was looking for. By using the cordova.file.externalDataDirectory, I was able to store the files on internal file system (not to SD card) and see them in the file explorer.
I think that the assumption of external storage referring to SD card only isn't totally unreasonable since the File plugin has / as a directory for cordova.file.externalRootDirectory in file system layout for Android. This must have been the thing that made me think that way.
I'm geting a file from server, and storing it on the phone. It's a PDF. Then I need to display . Assuming that I have a PDF viewer it will open the file.
The question is where should I store the PDF file so my pdf reader has access to it. I don't really want use external storage since not all phones has one. Is there a way to save public file on internal storage?
Or there is some way to pass the necessary information using ContentProvider. Unfortunately I would need some sample code of that.
All you put under internal directory can't be access by other applications. so getDir or getCacheDir have to be used only for your application.
If you need files to be open by other application you have to write files under SD card.