In my application there is a menu item, when you click on which a specific folder should open in the system file explorer android.
I would like the menu item to be able to open the desired folder, just like the browser does with the downloads folder after successfully downloading the file. But. As I understand it, this is not easy on Android. I need to cover API level 21-30 inclusive. For now I'm testing with API level 26 and there were problems.
First I had this:
string location = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;
if (location.Contains("/Android")) {
location = location.Split("/Android")[0];
}
location += "/Exports container";
if (!Directory.Exists(location)) {
Directory.CreateDirectory(location);
}
Intent intent = new Intent(Intent.ActionView);
var contentURI = Android.Net.Uri.Parse("file://" + location);
intent.SetDataAndType(contentURI, "application/*");
StartActivity(intent);
However I received a error: file:///storage/emulated/0/Documents/Exports container exposed beyond app through Intent.getData()
.
I edited (using https://stackoverflow.com/a/38858040):
string location = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;
if (location.Contains("/Android")) {
location = location.Split("/Android")[0];
}
location += "/Exports container";
if (!Directory.Exists(location)) {
Directory.CreateDirectory(location);
}
Intent intent = new Intent(Intent.ActionView);
var contentURI = FileProvider.GetUriForFile(Application.Context, Application.Context.PackageName + ".fileprovider", new Java.IO.File(location));
intent.SetDataAndType(contentURI, "resource/folder");
intent.SetFlags(ActivityFlags.NewTask);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
try {
StartActivity(intent);
} catch (Exception ex) {
Toast.MakeText(Application.Context, ex.Message, ToastLength.Long).Show();
}
However, I got a new problem: error APT2260: resource xml/provider_paths (aka com.vtbuild17.demoapp:xml/provider_paths) not found.
My Android manifest at that moment:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.vtbuild17.demoapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:icon="@mipmap/app_logo">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.vtbuild17.demoapp.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
provider_paths
is located here:
D:\demoapp\Resources\xml\provider_paths.xml
And now, it seems, I am waiting for a long "trial and error method" under the topic "where to put this file so that the error goes away". However, I'm not even sure that this solution will work.
So, my question: in what exotic places do I need to add files and what code to write so that, as a result, when the function is called (in the UI thread), the Android system (or any other) explorer with a open folder Exports container
opens in full screen?
UPD:
Content of the provider_paths.xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
UPD: Demo source code is availible here: repository.