StartActivity does not launch new intent in Android 11 - API 30 version

Keerthi 1 Reputation point
2021-08-17T10:58:35.3+00:00

The following code snippet seems to be working fine to launch the saved file, in Andriod 9 version.

Context context = Android.App.Application.Context;
if (file.Exists())
{
Android.Net.Uri path = Android.Net.Uri.FromFile(file);
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionOpenDocument);
intent.SetDataAndType(path, mimeType);
context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}

But this code throws errors in Android 11 - API 30 and does not launch the file.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,471 Reputation points Microsoft Vendor
    2021-08-18T07:31:06.427+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Based on my research, I find this issue is related this line: Android.Net.Uri path = Android.Net.Uri.FromFile(file);.

    After android 7.0, You should use FileProvider to generate the Android.Net.Uri.

    Firstly, If you want to use it. please add <provider> code in your AndroidManifest.xml like following code.

       <?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.companyname.startactivitydemo11">  
           <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />  
           <application android:label="StartActivityDemo11.Android" android:theme="@style/MainTheme">  
             <provider  
                android:name="androidx.core.content.FileProvider"  
                android:authorities="com.companyname.startactivitydemo11.fileProvider"  
                android:grantUriPermissions="true"  
                android:exported="false">  
         
               <meta-data  
                  android:name="android.support.FILE_PROVIDER_PATHS"  
                  android:resource="@xml/file_paths" />  
             </provider>  
               
           </application>  
           <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
       </manifest>  
    

    Then create a xml folder, add file_paths.xml.

    124187-image.png

    add following code.

       <?xml version="1.0" encoding="utf-8" ?>  
       <paths xmlns:android="http://schemas.android.com/apk/res/android">  
             
             <external-path name="external_files" path="."/>  
           
       </paths>  
    

    Here is my edited code.

       var documentsDirectry = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDocuments);  
         
         
                   Java.IO.File file = new Java.IO.File(documentsDirectry, "icon1.txt");  
                   file.CreateNewFile();  
                   Context context = Android.App.Application.Context;  
                   if (file.Exists())  
                   {  
                       Android.Net.Uri path = FileProvider.GetUriForFile(global::Android.App.Application.Context, global::Android.App.Application.Context.PackageName + ".fileProvider", file);  
         
                       // Android.Net.Uri path = Android.Net.Uri.FromFile(file);  
                       string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());  
                       string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);  
                       Intent intent = new Intent(Intent.ActionOpenDocument);  
         
                       intent.SetDataAndType(path, mimeType);  
                      
                       context.StartActivity(Intent.CreateChooser(intent, "Choose App").AddFlags(ActivityFlags.NewTask));  
                   }  
    

    Here is my running screenshot.

    124159-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Ed Rabenda 1 Reputation point
    2021-08-23T17:10:58.657+00:00

    I was trying to save a file.xlsx but could not find the file in Open From - why not?

    0 comments No comments

  3. Ed Rabenda 1 Reputation point
    2021-08-23T17:10:58.683+00:00

    I was trying to save a file.xlsx but could not find the file in Open From - why not?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.