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
Related
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.
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)
My question is pretty general; whenever an android app accesses internal storage environment path; its folder structure is created into device's "Android->Data->app_pakage".
Image files within this folder won't be viewed from gallery; which is fine.
There are lots of app which secures this files (Can't be opened directly from file manager);
Examples for this are music apps; they provide encoding of its downloaded files, so it can be accessed only from that app only. This encoding changes its extension
So my question is here, How this can be achieved?. I am looking for simplest solution, which don't require a high, complex encryption algorithms.
In-Short, I want to prevent users to access app files from file manager- internal storage
Please have a look at this link
https://developer.android.com/training/data-storage/files.html
Here there is detailed explanation.
To have files that can only be accessed from your app please use -
getFilesDir()
Returns a File representing an internal directory for your app.
Files saved here are accessible by only your app.
Can anyone confirm that the documents directory file storage method with Flutter is secure or whether the AppData directory is where/how Android stores its Internal Storage files?
I'm looking at storing some persistent local data on the device but I want to make sure the data I write is not plain text or accessible by anyone/anything else. If this was a regular Android application, I'd be using Android's Internal Storage which says data stored is "private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed."
Flutter has its own platform-agnostic way to read and write files and its documentation says saving things to its documents directory stores files "only it can access. The system clears the directory only when the app is deleted. On iOS, this corresponds to NSDocumentsDirectory. On Android, this is the AppData directory."
It looks like these are talking about the same thing and would therefore meet my security criteria, but these are things I'm not very familiar with and I don't want to take a risk with my users' data. I tried googling to find out what gets saved in the "AppData" directory on Android but mostly found people talking about their Android Studio installations.
Yes, NSDocumentDirectory on iOS and AppData on Android are secure locations.
This line from the example gives you the correct path for storing files which can only be accessed by your app:
String dir = (await PathProvider.getApplicationDocumentsDirectory()).path;
On Android dir resolves/data/data/com.yourcompany.AppName/. On iOS devices the folder is /var/mobile/Containers/Data/APP_ID/Documents.
Check the Android Security Tips , the section on Internal Storage:
By default, files that you create on internal storage are accessible
only to your app. Android implements this protection, and it's
sufficient for most applications.
The exception here is that when your app runs on a rooted Android device, the app data folder is not secure any more, see https://stackoverflow.com/a/8184699.
I have a project consisting of four programs for different platforms; all of them use the same XML-based settings file format. I want to be able to manually modify/overwrite it outside of the application. On Windows, Windows Mobile and Linux I'm using "user.home", but on Android that alias isn't implemented. I'm thinking about simply putting it in the Downloads directory, however, that doesn't feel right.
I can't be the only one, who needs that kind of functionality. Or this isn't Android-way? Any suggestions are appreciated.
EDIT: I'm OK with the settings file not being available all the time (i.e. SD-card removed), it's used only on the start-up of the application.
Store it in getExternalFilesDir(). This would work only if the device has an external storage. The user would be able to access it.
However, take note of the following from the docs:
External files are not always available: they will disappear if the
user mounts the external storage on a computer or removes it. See the
APIs on Environment for information in the storage state.
According to Android data storage documentation you have 5 options:
Shared Preferences. By default this will use file /data/data/your.package.name/preferences/user_preferences.xml
Internal Storage. Here you can use something like /data/data/you.package.name/user.home
External Storage. Similar to internal storage /mnt/sdcard/Android/data/your.package.name/user.home, but if user removes memory card file will be inaccessible.
SQLiteDatabase. You can store the whole user.home file in a database blob.
NetworkConnection. Store user's config in a cloud.