How to copy files in external storage in Android Q - android

I made a app(is called A) without UI which provides some information to other clients app with AIDL.
So I want to make an UI app(is called B) for displaying A apps status and send some files to server(logs, db, etc...).
However, I'm having some problem with Scoped Storage in Android Q.
(I know about android:requestLegacyExternalStorage="true" but I don't want to use this trick)
I'm looking for MediaStore and SAF(Storage Access Framework) but I'm not sure which one is right and something more(what I didn't think)
Could I use MediaStore for copying DB files to external storage(actual public space for other apps)?
Could I use SAF even an App A doesn't have any UI?
(and how?)
Thanks for helping me.

Just request that legacy external storage.
It is not a trick. And quite handy for an Android 10 device. It works only on an Android 10/Q device.
which provides some information to other clients app
In which way?
You could use a content provider for that.

Related

How to save .csv in public location on Android 11 phone?

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?

How to implement New Storage API in Android 11?

Scenario
I've two apps, one is the Tracker App which records the incoming and outgoing calls and zipped these files, and sends the file path to the Main App via Inter-Process Communication which uploads these files to the server.
Now I'm upgrading both apps to Android 11. In Tracker App, I'm using MediaStore.Files API to save files and trying to read these files using the file path in Main App. While reading file File.canRead() returns false in Main App. Even I tried MediaStore API to read these files it returns empty Cursor.
Here I've few questions.
Can I read files that are created by the Others app on Android 11? I read somewhere that you can't access others apps files in Android 11.
Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?
What will be the best way to handle this scenario?
Can ``` Storage Access Framework `` help me to handle this scenario?
I'm saving files in the public directory Documents/AppData/Audio. Please give me working links regarding this. Any help will be appreciated. Thanks
As in Android 11 MediaStore API only returns media files.
so, i will answer your questions related to it.
Can I read files that are created by the Others app on Android 11? I
read somewhere that you can't access others apps files in Android 11.
No! you can't access the files created by other application that are stored in personal storage of that specific app.
Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access
all files in storage?
As far as i understand, your app doesn't required external storage to store the data it can be also done in private storage where you can read or write your data. and if your app is eligible for this permission anyway still you can't use it as of now. it's been suggested by official web rather then asking this permission make target API 29 and use android:requestLegacyExternalStorage="true" in your manifest.
click here to read about it.
What will be the best way to handle this scenario?
Rather using external audio path you can use you app specific folder to store the archive that you are creating.
Can ``` Storage Access Framework `` help me to handle this scenario?
I don't have much idea about how IPC work between two apps so i can't tell exactly that it will be better to use Storage framework.
I've two apps, one is the Tracker App which records the incoming and outgoing calls and zipped these files, and sends the file path to the Main App via Inter-Process Communication which uploads these files to the server.
That seems overly complex.
Can I read files that are created by the Others app on Android 11?
Technically, you are not writing a file. You are creating an entry in MediaStore.Files. Other apps cannot read entries that you create in MediaStore.Files.
Is my app eligible for MANAGE_EXTERNAL_STORAGE permission to access all files in storage?
We are not Google. We have no way of answering that. It would surprise me greatly, though, if they considered your app to be eligible for this.
What will be the best way to handle this scenario?
Well, IMHO, the best way by far would be to have one app, not two. But I am assuming you are not in position to change that at this time.
If so, then:
Have the "tracker" app write the content to files in a filesystem directory that the app can write to. Mostly, that will be via methods on Context, such as getFilesDir() or getExternalCacheDir().
Have the "tracker" app use FileProvider to serve files from that directory, and use FileProvider.getUriForFile() to get a Uri pointing to that file.
Have the "tracker" app invoke your "main" app via some Intent-based mechanism (startActivity(), startService(), sendBroadcast(), etc.). Put the Uri from the previous bullet into that Intent, and add FLAG_GRANT_READ_URI_PERMISSION to that Intent.
Have the "main" app read the content using a ContentResolver and openInputStream(), passing in the Uri that it extracts from its copy of the Intent.
Can ``` Storage Access Framework `` help me to handle this scenario?
You could have the user, in each app, use the Storage Access Framework. For example, you could use ACTION_OPEN_DOCUMENT_TREE in each app, hoping that the user would choose the same tree in each app. Personally, I would use FileProvider, as it does not require user interaction and does not require the user to make good choices.
The receiving app can use SAF to let the user pick your directory.
Or more standard: you have files so you build your own file/content provider to serve your files.
And if you use inter process communication(how by the way) you could serve your files one by one using the uri from mediastore and FileProvider.

How to write to the external storage in android that will be present even after the application is uninstalled?

I am trying to build an application in which user can share media files over internet. Now I am confused that there are lots of functions in the library through which we can get access to files stored in the users storage. I don't know which to use..
In Context#getExternalFilesDir(String) documentation, they insist using Environment.getExternalStoragePublicDirectory() to write media that must be shared with other apps and that must be kept even after uninstall of an app. But in Environment#getExternalStoragePublicDirectory(string) documentation, they say that this method is deprecated, and recommend using alternatives such as Context.getExternalFilesDir(). If that was deprecated in first place, then why did they insist to use that method. What is the reason behind them playing these tricks, I don't understand.
Please suggest me a function that is: not deprecated, should return a directory where I can read and write, media stored there should be visible to other apps, they must not be deleted when user uninstalles this app.
I believe you are looking for Shared Storage.
Android file system and storage changed a lot during this years.
Data and file storage overview

Android global writable folder

I'm trying to put firebird embedded running on android.
The problem is that fb put temporary files on /data/local/tmp.
That folder don't seems to have write permissions, at least on Android 6, and fb fails to start.
Do android have a good place to put this files?
That place must be available globally and can't be cleaned by external services.
Regards
jGoncalves
That place must be available globally and can't be cleaned by external
services.
Those two things are mutually exclusive.
You can either put it in external storage where any app can read and write it (if they've been granted the respective permissions), or you can put it in app private storage where only your app can read it.
If you want to export data from a database to other apps, the right way is to wrap it with a content provider and publish your content provider interface to other interested apps.
I don't know Firebase, but I'm guessing that it can't support multiple processes reading and writing to the same DB file.

Is there any permanent non removable global storage available for the applications on each android device ?

I need to store the data on the android device and want to access this data from different applications(Through the services in background).
We can achieve this using the External storage. But it may possible that if it is removable then it will not be available when mounted to PC or removed. So, Is there any permanent non removable global storage always available for the all applications on each android device ? If so how I can access it.
Please guide me. Thanks in advance.
But it may possible that if it is removable then it will not be available when mounted to PC or removed.
"mounted to PC" is not a problem on Android 3.0+, though it is on Android 1.x/2.x. And external storage is rarely removable on Android 3.0+ devices.
So, Is there any permanent non removable global storage always available for the all applications on each android device ?
External storage is as close as you are going to get. This means that you may wish to reconsider your original plan. Depending upon the nature of your app(s), the answer may be that one app has the data on internal storage and is making the data available to other apps in your suite via a ContentProvider. This has more fine-grained security possibilities than does external storage.
File system storage will not help you as everywhere your apps can read/write every other app can read/write/delete, too and you end up in a similar situation as on the sdcard section.
BTW: Unmounting the sdcard section when connecting to a PC only works on onld outdated devices. Current devices just use the MTP protocol, which allows access from Android and PC at the same time.
If several apps are working together there a many ways for sharing data among each other:
Use the sharedUserId feature in each app (AndroidManifest.xml).
Make one "storing-app" that has one or more Provider for accessing the data from different apps. Secure them using a self-defined permission.
You can use the internal storage with FileOutputStream, as it's explained here in the documentation.
By default, those files are private to your app, but you can change that with the Context.MODE_WORLD_WRITEABLE or Context.MODE_WORLD_READABLE.
But please, be careful with that, as it can cause some security problems on your app. Perhaps you could use Intents or other ways to make that link between your apps.
For more information on that, please refer to the documentation.
Every device running android 4.0+ is based on MTP (MEDIA TRANSFER PROTOCOL) which means that if you mount your device to the PC, it will still be available to your application, so don't worry about it too much. You must have read some outdated articles about this.
EDIT: According to the answer of CommonsWare, it's 3.0+ even, but devices running Android 3.x are so rare they are basically dead.
For lightweight primitives (strings/ints, etc), you can write to system settings, which is hard to remove by end users.
You can expose app's data by using flag Context.MODE_WORLD_READABLE.
Use ContentProvider for structed data. Use the authorities and permissions so that only your apps are allowed to read/write to content provider.
External storage, but as you said, is not reliable. The data on it are public and visible to all apps and also to end user. The data are easily deleted by users and some cleaning app.

Categories

Resources