Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Android 10's Storage Mess

As Android is very inconsistent between different major Versions regarding File access, I feel a bit lost. I try to describe the problem as easy as possible:

My Company uses a commercial native DRM to protect other native library's we provide. We have a Licensing App, which invoked some Voodoo to end up with Licensing files in say /sdcard/companyname/LicenseContainer/. Other protected Apps looked at this directory in native code, checking if the user has a valid License.

The Android 10 update however, invalidated this workflow completely as it only provides scoped storage access. We could do a workaround using Storage Manager to grant access, which is unfortunately also deprecated now.

So my Question is now: How can one App save files to a location on /sdcard/FOLDER which are

  1. not deleted on App deletion
  2. Accessible in native code by other apps

I'm a bit overwhelmed with all the possible solutions (SAF, FileProvider, etc), which invoke often that one app grants permissions to the other. But the files should be accessible without an installed first app who put it there.

I know there must be a solution, as recent FileManagers (i.e. Files by Google) get access to the whole /sdcard/ directory.

Whats the easiest, future-proof route to go here without invoking "hacks" like android:requestLegacyExternalStorage="true"

like image 371
Rafael T Avatar asked Sep 05 '19 15:09

Rafael T


People also ask

How to fix Android storage space running out problem?

Using the simple steps listed below you can deal with Android storage space running out problem: Open your Android device. This will bring up a menu with various options. Select the second option named Delete dumpstat/logcat and tap on it. Tap OK in the pop window and your work will be done. 3. Move apps to SD Card

How do I Find my Storage Settings on my Android phone?

On your Android phone or tablet, swipe down from the top of the screen once or twice, then tap the Gear icon to open the “Settings” menu. Next, select “Storage” in the “Settings” menu. On a Samsung Galaxy phone, you will need to tap “Device Care” first to get to the “Storage” option.

How do I know what is taking up space on Android?

Inspect your storage for the ‘heaviest’ files Android phones have a built-in Storage tool that will give you an insight into what is exactly taking all that space on your storage. It is good enough for the classic clean up of the drive but won’t show all the details.

How to fix “storage is full” error on Android?

Over an extended period of time, it tends to fill up. And then some users are unable to update apps or system firmware on Android due to the “Storage is full” error. Today, we’ll show you how to simply address this problem by making some room in your internal storage. 1. Use Cloud or PC storage to relieve some storage space


Video Answer


1 Answers

You may ask the user to give you access to any file or directory, including the root of internal storage or external SD card. You can make this access permanent for your app, be able to read/write files anywhere with the Scoped Storage API afterwards, until the app is uninstalled or reset.

Then, if you need to read or write a file in native C/C++ code, you may get Linux file descriptor (int number) of the file and pass it to native code to use with fdopen() call for example.

Here is a Java code snippet to get a file descriptor form a single file Uri (which in string form is like content://...)

    ParcelFileDescriptor parcelFileDescriptor =
            getContentResolver().openFileDescriptor(uri, "r"); // gets FileNotFoundException here, if file we used to have was deleted
    int fd = parcelFileDescriptor.getFd(); // detachFd() if we want to close in native code

If you have source code for your native libraries, or can call them with C FILE* - it will work fine. The only problem is when you don't have the source code and they expect a file path/name. * UPDATE *: it is still possible to use the path/file name strings to pass to C/C++ functions that expect a file name. Simply instead of the "real path/file name", create a name to symbolic link like this:

// fd is file descriptor obtained in Java code above
char fileName[32];
sprintf(fileName, "/proc/self/fd/%d", fd);
// The above fileName can be passed to C/C++ functions that expect a file name.
// They can read or write to it, depending on permissions to fd given in Java,
// but I guess C/C++ code can not create a new file. Someone correct me,
// if I'm mistaken here.

However, at this time I'm not sure that when you create a file in a directory beyond the app "sandbox" in that way, if the system will delete this file too after uninstall... Would need to write a quick test on Android 10 to find out, and then we still won't know if Google won't change this behavior in future.

like image 185
gregko Avatar answered Oct 06 '22 00:10

gregko