0

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:

enter image description 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.

Alex A.
  • 422
  • 3
  • 12
  • What's the code of your `provider_paths.xml`? If it is convenient for you,could you please post a basic demo so that we can test on our side? – Jessie Zhang -MSFT Aug 30 '21 at 07:54
  • @JessieZhang-MSFT Basically, my demo is easy to reproduce, but I still created it again and put it in the repository. Now, regarding the question, I think I have provided everything that was possible. – Alex A. Aug 30 '21 at 19:03

1 Answers1

0

The Build Action of your provider_paths.xml is nor right.

Try to change the Build Action of your provider_paths.xml to

Android Resource

instead of

AndroidResourceAnalysisConfig
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Your advice helped clear up the error. However, the application is still not going to open the folder I'm interested in. – Alex A. Sep 01 '21 at 21:44
  • When I edited the code like this: ``` Intent intent = new Intent(Intent.ActionView); var contentURI = FileProvider.GetUriForFile(null, Application.Context.PackageName + ".provider", new Java.IO.File(location)); intent.SetDataAndType(contentURI, "application/*"); ``` The program did not crash, but it also did not open the folder. Among the programs that Android offered, there was no file explorer at all. – Alex A. Sep 01 '21 at 21:46
  • When I selected, for example, a browser, there was something strange in its address bar: `content://com.vtbuild17.demoapp.provider/external_files/Documents/Exports%20container` This path clearly does not lead to the correct folder. – Alex A. Sep 01 '21 at 21:48