I have a simple one activity app that creates a CSV file, appending rows from edittexts using one button. The intention is to gather data on this phone app and then process the CSV elsewhere.
I have developed the app in android studio using context.getExternalFilesDir(). It works great except the CSV is not available in a public location once moved onto a phone. I need to CSV to be available via a Files browsing app and email app on the phone. Ideally I would like to put it in the documents folder (either on phone or card) but any pucblic directory will do!
I read it is recommended to use the Storage Access Framework but I understand this will demand user interactions. This is a simple convenience app that will be constantly used for 6 months, so requiring regular user interactions to browse to locations and grant permissions is not acceptable.
Or can I use SAF without involving user interaction? Alternatively, I suspect I may be able to target an older SDK but have no idea if that is correct and how would that work on the phone running Android 11? Or can I use the Media API to save a locally made text/csv file? Wow, it's a minefield. Any help much appreciated.
This app will never be available on play store. It will only ever be installed on one phone. My app use Kotlin so a Kotlin example would be the best!
My question: how can I save the csv in a public location on an android 11 phone without requiring constant user interaction?
Related
The application I'm currently working on requires a manual setup (entering some information) on device provisioning. This information needs to be written to a file that should not be deleted when the application is uninstall or the application data is wiped (user support requirement, as they can direct users to do this in some cases)
There was a very similar old question, but the answer is now deprecated and no up-to-date answer has been posted
Keep files after uninstallation of android app
So the question is, given the deprecation of Environment.getExternalStorageDirectory() on Android 10, how do we programmatically write/read a file that will not be deleted when the application is uninstalled or the data is wiped?
For what is worth, we can not rely on app auto backup, as the users don't have google accounts configured.
Thanks
To summarize while targetting 30.
For Android 10 device: Request legacy external storage to get external storage access as usual.
And Googles step back for Android 11 devices: use directories like Download, Pictures, Movies, Documents, DCIM and so on. Read and write access for all. Android OS is very picky to use the right extensions for files to be created in those folders.
My company has three different Android apps that provide functionality for sales reps. We opted to separate the apps into "modules" because not all reps need all the modules. Up to now we have been using a JSON file in a directory on the SD card of the devices to set some configuration data for the apps. However, it appears that with Android 10 and beyond this will no longer be possible.
Currently we use getExternalStorageDirectory() to access the SDCARD and then open a file inside a directory our app creates.
Since we want the file access to not be something the sales reps have any control over we want it to happen transparently. However it does not seem this will be possible going forward.
Will using a custom FileProvider or even a DocumentsProvider be a way to continue to share data between our apps?
Another question, although not as important, is, can we change the default location of the DB files our app creates as we do now in earlier versions of Android?
Thanks
Rich
I'm developing application that is targeted for API 18. Application download data by Bluetooth and GSM and stores data on internal sdcard (usually \emulated\sdcard - readed by Environment.getExternalStorageDirectory()).
Till this time everything works fine - files and folders are correctly saved.
Today i've faced very strange behaviour:
1) Started the app, and it works for over an hour and store data files.
2) After that, i've closed app and want to download data to the computer.
Before plugin USB, I've used Android file explorer software to check stored data.
I was shocked - new folder (any new data) was gone!
It's like system removes or hide all files and data created in last app session.
Remarks: application don't have procedures for deletion folder or files. Also it works good on previous versions of Android (mostly 4.X and 5.X).
It's very strange because app has warning mechanism when save isn't possible and that warnings wasn't displayed. So I think, write was allowed by system, but data isn't visible at this moment.
Another stranger thing is that application can't write any file in internal storage from this moment (and the warnings are displayed as should in this case).
It looks like Android Marshmallow "decides" in particular moment - your new data won't be available anymore, and your app won't write to internal card.
I've checked app permissions in system - OK.
Any ideas, why this happens? How to deal with it?
You may refer to these guides for managing permissions during runtime: http://www.howtogeek.com/230683/how-to-manage-app-permissions-on-android-6.0/
https://developer.android.com/training/permissions/requesting.html
background
Starting with version 4.2 , Android supports multi-user (link here and here).
Each user has its apps and their private data is visible just for the user.
The question
How does the encapsulation of the data per user work in terms of paths and accessing files?
I mean, what will be the paths per each user for:
the private, internal storage.
the emulated external storage (built in external storage)
the "real" external storage (sd cards)
?
I guess users can see the data stored on the sd cards that belong to other users, but what about the emulated external storage? And can they also write other users' files or just read them?
Does each user get its own special path automatically? or should the developer handle this?
If the developer needs to handle it, what should be used as the ID of the user?
The documentation says:
No matter which of these APIs you use to save data for a given user,
the data will not be accessible while running as a different user.
But that's all assuming you use the APIs for your own path. Could apps somehow bypass this by going to other paths?
What can an app query about each installation of itself on the same device? Can they get the size of apps of other users? Can they even get the list of apps of other users?
Does multiple installation of the same app also take multiple size?
what will be the paths per each user
If you care, you're doing it wrong. Use the Android SDK APIs for determining base directories, and work from there. So, for example:
getFilesDir() will return the right location for internal storage for the current user
getExternalFilesDir() and the methods on Environment will return the right locations for external storage for the current user
I guess users can see the data stored on the sd cards that belong to other users
That is outside the bounds of the Android SDK, generally.
but what about the emulated external storage?
Each user gets their own space.
And can they also write other users' files or just read them?
Neither, barring bugs in the device.
Does each user get its own special path automatically?
Yes, if you are using the Android SDK APIs for determining base directories.
Could apps somehow bypass this by going to other paths?
No, because they will have neither read nor write access, barring bugs.
What can an app query about each installation of itself on the same device?
AFAIK, nothing. From the app's standpoint, the fact that there are several installations on one device is indistinguishable from being installed on several devices.
Can they get the size of apps of other users?
I do not know what you mean by "size of apps", sorry.
Can they even get the list of apps of other users?
That's a fine question. I have not experimented with PackageManager to see what it exposes when used by apps run by secondary users. In theory, it should only report things that are available to the current user, particularly given Android's restricted profiles.
Does multiple installation of the same app also take multiple size?
The APK and the pieces of it that are unpacked (e.g., DEX files) are shared, as I understand it.
Android introduced the Multiple Users feature in 4.2 (Jelly Bean MR1) and its documentation states:
From your app’s point of view, each user is running on a completely separate device.
And here is a quote from the Environment.getExternalsStorageDirectory() and getExternalStoragePublicDirectory() methods doc:
On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.
Could it be true that there really is no reliable way to communicate data between users on a single device without using the network as mediator? I'm looking for solutions that don't rely on quirks of how the device's file system is laid out by a manufacturer. Also, for security, the sharing should be internal to my app.
Even if file sharing is indeed impossible, is communication via intents somehow possible?
There are use cases for this. Use Case 1: let's say I'm writing an input method app that requires a 100MB dictionary file. I'd like to code things so that if User A downloads this file, then User B can access it also without needing to re-download. Use Case 2: let's say I'm writing a fun Leave My Wife a Note app that allows User A to type messages that will appear next time User B logs in (without using the network).
This thread on a separate site proposes a solution, but their method seems undocumented and possibly unreliable. And there are a few other SO questions that have a title similar to this one but are actually discussing different topics.
OBB Folder (/sdcard/Android/obb) is used to share files and folder between the multi users. But OBB folder not shown in my second user (One plus 5 mobile). So I have tried to create an OBB folder in Android folder (/sdcard/Android/) in second user and "BOOM" it worked. Now i am able to access the shared files in second user. Try this trick if OBB folder not shown in your second user.
OBB files (stored in /sdcard/Android/obb) and used as expansion files in Google Play are shared between all users by design, as they are fairly large. If you Input method uses expansion files, the downloaded data will be shared automatically. You can send broadcasts to other users but that requires the INTERACT_ACROSS_USERS permission, which is reserved for system applications.
I also had the same question, and have tried various approaches such as using /sdcard/Android/obb but it does not work in Android 10. So I followed below approach, and I am able to copy files seamlessly between users.
Login to the User from where you would like to copy files from (lets call U1)
Run FTP Server using any application of choice like MiXplorer / ES Explorer etc... Note down the details of the port#, username, password etc... and point it to /sdcard
Switch user, to where you want to copy files to (lets call U2)
Install the FTP browser. If you use MiXplorer / ES Explorer, they will allow you to add a FTP share
Use ftp://localhost:2121 assuming the port is 2121, if not change it accordingly and add the FTP share
Open the FTP share and you can see all the files & folders of U1 here
Copy across to your heart's content !