Forum Xamarin.Android

Open file with my app

Hi,
I want my app to export data to the downloads directory.

To get the downloads directory, I use this code
string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;

The path variable has the value "/storage/emulated/0/Download".

When I save my file to Path.Combine(path, "myfile.ext"), the file is being saved correctly.

To open that file with my app, I created an Activity ImportActivity with an attached intent-filter.

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="*" />
    <data android:mimeType="*/*" />
    <data android:pathPattern="/.*\\.ext" />
    <data android:scheme="file" />
    <data android:scheme="content" />
  </intent-filter>

Now, when I use the file browser (the one from Samsung) and navigate to the downloads directory, I can see my file.
After tapping on said file, my ImportActivity is being created and I get the file path "/device_storage/Download/myfile.ext".
When I'm trying to get a corresponding FileStream, I get an exception that the specified folder can't be found.
That's my first problem.

My second problem is that after just viewing the download folder's content in Windows Explorer or after a device reset, myfile.ext won't be opened with my app. The file association seems to be gone.

How can I solve these problems?

Answers

Sign In or Register to comment.