Android Q Accessing External files.

JohnPrassJohnPrass Member ✭✭
edited September 27 in Xamarin.Android

** Noticed that this is more of a Xamarin Forms question, moving it to Xamarin Forms Thread.

I have an app with a feature to backup and restore its database. I create a file and saved it in external storage using this path:

return Android.OS.Environment.ExternalStorageDirectory.AbsolutePath

and allow the user to navigate to the directory they wish to save the file.

Everything worked exactly as I needed it to. I'm currently upgrading to the Android Q 10.0 framework and there are changes to external storage that no longer allows me to access this path. I'm looking into the private external storage, but I can't figure out a way to allow what I need.

Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath

Using the GetExternalFilesDir doesn't seem to persist uninstall.

Here are my objectives:

  1. I would like to be able to write the backup file, uninstall the app, re-install the app and restore from the backup.
  2. backup from current device, Install app on new device, copy file to new device, and restore the backup.

Any thoughts on how this can be achieved in Android Q 10.0? Any help will be greatly appreciated.

Answers

  • LeoZhuLeoZhu Member ✭✭✭
    edited September 30

    first Android.OS.Environment.ExternalStorageDirectory return Public external files that exist on external storage that are not stored in the directory that Android allocates for private files. Public files will not be deleted when the app is uninstalled. Android.App.Application.Context.GetExternalFilesDir return Private external files that are considered to be specific to an application (similar to internal files) but are being kept on external storage for any number of reasons (such as being too large for internal storage). Similar to internal files, these files will be deleted when the app is uninstalled by the user.

    second ,Android Q file storage mechanism has been modified to sandbox mode, similar to IOS,you can only access files in your own sandbox and public media files.Operations on a common file require the ContentResolverand Cursor

    so if you want to save the file and keep it from being deleted when the app is uninstalled,you may save it to public external storage,the more you could refer to Save a file on external storage

  • JohnPrassJohnPrass Member ✭✭

    @LeoZhu, thank you for your response, this is exactly what I've learned with my research, but it still doesn't answer my question. Using Android Q, what is the proper way to save off files so they can later be used after an uninstall/reinstall situation? (a standard backup of app data) Your last paragraph mentions the link to "Save a file on external storage" which clearly states that writing to public external storage is being depreciated. I fully understand the reasoning, but I need a way to store a backup that can be later accessed to restore data to my app. Is it possible for a user to move a file from the sandbox to another folder of their choice, and then back to an app sandbox after a reinstall? That would satisfy my requirement, but I cannot find a way to move a file from public storage to an app sandbox. A feature of my app is privacy, so I don't want to have users use an online backup. I have to be missing something here, surely Android hasn't made it impossible to backup app data locally that can withstand the necessity of a uninstall/reinstall.

  • LeoZhuLeoZhu Member ✭✭✭

    @JohnPrass If your code refers to Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory(), you will see that they are deprecated. They still work, but the deprecation warning is yet another nudge to remind you that you need to stop using those.On Android Q,you could use Save to a public directory and refert to media store

  • JohnPrassJohnPrass Member ✭✭

    Does anyone else have any thought on this? Looking at the links on developer.android.com they mention using a ACTION_CREATE_DOCUMENT intent. Will this still work on android Q to write to public external storage?

  • LeoZhuLeoZhu Member ✭✭✭

    For Android Q, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    Did you find an answer for this, I want to save a custom voice recording sound and use that sound for push notifications. I used to store in external directory by:

    var recordingFileExternalPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, AppConstants.CUSTOM_ALERT_FILENAME);

    and then from there get the Uri and set it for the sound for that notification channel.
    Does not work in Android Q

Sign In or Register to comment.