Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin forms android app : Calling startActivity() from outside of an Activity context

CrossSharpCrossSharp Member ✭✭
edited August 2019 in Xamarin.Android

Hello , I am trying to open a pdf file using filechooser in Android Xamarin forms app, my code throw the exception :
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag in android 9 but works for anyversion less than 9

this is my open PDF method :

public void OpenPdf(string FilePath)
{
Java.IO.File JavaFile = new Java.IO.File(FilePath);
Android.Net.Uri Path = FileProvider.GetUriForFile(global::Android.App.Application.Context, global::Android.App.Application.Context.PackageName + ".fileprovider", JavaFile);

        string Extension = "pdf";
        string MimeType = global::Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Extension);
        Intent Intent = new Intent(Intent.ActionView);
        if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
        {
            Intent.AddFlags(ActivityFlags.GrantReadUriPermission);
            Intent.AddFlags(ActivityFlags.NewTask);

            Intent.SetDataAndType(Path, MimeType);

            try
            {
                Android.App.Application.Context.StartActivity(Intent.CreateChooser(Intent, "OPEN PDF"));
            }
            catch (Exception ex) // The exception is thrown here
            {
                Toast.MakeText(Android.App.Application.Context, "ERROR !"+ex.Message, ToastLength.Short).Show();
            }
        }
        else
        {
            try
            {
                var uri = Android.Net.Uri.Parse("file://" + FilePath);
                var intent2 = new Intent(Intent.ActionView);
                intent2.SetDataAndType(uri, MimeType);
                intent2.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                intent2.AddFlags(ActivityFlags.MultipleTask);
                try
                {
                    Forms.Context.StartActivity(Intent.CreateChooser(intent2, "Open PDF FILE"));
                }
                catch (Exception ex)
                {
                    Toast.MakeText(Android.App.Application.Context, "ERROR !" + ex.Message, ToastLength.Short).Show();
                }
            }
            catch (Exception ex)
            {
                Toast.MakeText(Android.App.Application.Context, "ERROR !" + ex.Message, ToastLength.Short).Show();
            }
        }

    }

Can someone please guide me with the correct way to open a file please !

Answers

  • CrossSharpCrossSharp Member ✭✭

    I solved this problem with :

    var chooserIntent = Intent.CreateChooser(Intent, "Open PDF");
    chooserIntent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);:
    Android.App.Application.Context.StartActivity(chooserIntent);

    correct me please if this is a bad approach I just found this somewhere in Github

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @CrossSharp
    I am gald to hear that, please mark your reply as answer, it will help others who have similar issue.

Sign In or Register to comment.