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.
Related
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?
I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder. However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG! :) folder on my Android 10 (R) phone. I've searched for 2 days now how to create a simple .csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location.
I'm setting my Android Studio to minSdkVersion at 16, complileSdkVersion to 28, so at least I can use some of the earlier methods for file management. I expect my app to run on phones/tablets at least 3 years old and newer. There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file, just simply create a file there is all I need. I'm an old Java programmer but new to Android. Most posts point to deprecated functions we lose in >28 SDKs.
I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder.
No, that is internal storage.
However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG! :) folder on my Android 10 (R) phone
That app will need to be updated soon.
I've searched for 2 days now how to create a simple .csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location
For Android 10 and below, you can use Environment.getExternalStorageDirectory() for the root of external storage. Note that for Android 10 itself, you will need to add android:requestLegacyExternalStorage="true" in the <application> element.
On Android 11+, you can no longer write to the root of external storage using filesystem APIs, unless you hold MANAGE_EXTERNAL_STORAGE. That permission will require justification for use, if you plan on distributing your app via the Play Store (and perhaps elsewhere).
There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file
The user may disagree with you. Google definitely disagrees with you. On Android 11, if you do not want your app to be banned from the Play Store, you will need to use ACTION_CREATE_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE and allow the user to decide where the user's file goes on the user's device.
Most posts point to deprecated functions we lose in >28 SDKs.
"Deprecated" in Android does not always mean "lose". It always means "there is another option that Google wishes for you to consider. So, for example, Environment.getExternalStorageDirectory() still exists, even in Android 11. It is just less useful, and Google would prefer that you use methods on StorageVolume instead.
I am building the Android 9 Pie GSI from the AOSP and flashing it into the PIXEL 3XL. I have my own application and I have added it to the Android 9 Pie GSI ROM - so this application builds together with the ROM. This application has privileged app permissions (located in /system/priv-app) and its data must be protected from the userdata wiping.
I tried to use the /persist for this purpose, but it's read only. I tried the /mnt/vendor/persist/ and I got the "permission denied" error.
What folder can I use to save the apps data?
The answer here at: https://android.stackexchange.com/a/47951/56147 will help. Please take a look at it. In summary the link says that:
"All apps (root or not) have a default data directory, which is /data/data/<package_name>. By default, the apps databases, settings, and all other data go here. If an app expects huge amounts of data to be stored, or for other reasons wants to "be nice to internal storage", there's a corresponding directory on the SDCard (Android/data/<package_name>)."
which I believe is mostly correct and should help set you on the right path. As you have root access, you can write anywhere, including where the user has no access.
Keep user state after app installation
https://developer.android.com/topic/instant-apps/ux-best-practices.html#keep_user_state_after_app_installation
I want to use the data stored in instant app, after installation full app.
Does anyone know it?
UPDATE They’ve again updated the Instant Apps FAQ on how to do this.
For devices running Android 8.0 (API level 26) or higher, the instant
app's data is transferred automatically when the APK installation
begins, if the installed app is configured to use targetSandboxVersion
2.
For installed apps not configured to use targetSandboxVersion 2, or
devices running Android 7.1 (API level 25) or lower, please consider
using the Cookie API (Sample) or Storage API (Sample) to transfer the
data.
For Oreo 8.0 and higher, it should be automatically handled.
But for 7.0 and below, you've got 2 options (#1 is recommended):
PackageManagerCompat#setInstantAppCookie(), the cookie has a size limit, but it is the simplest and most suitable for shared preferences.
InstantAppsClient#getInstantAppData(), creates a ZIP of the data that you'll have to extract and parse once it's moved over to the installed-app side, a bit more complicated.
The FAQ lists a sample for each option:
googlesamples/android-instant-apps/cookie-api
googlesamples/android-instant-apps/storage-api
The Storage API/ZIP sample does not show you how to parse the ZIP files and what file type/format to expect from SharedPreferences files, so you'll have to implement that yourself, but here are some related posts:
How to read file from ZIP using InputStream?
How to transfer sharedpreferences file from internal storage to external storage?
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