Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

how to show PDF from mobile storage in xmarin android c# app?

soumenhaldersoumenhalder Member ✭✭✭
edited February 14 in Xamarin.Android

I am working in a xmarin android app in c#. I want to open a pdf file which is in a custom folder of internal storage "TESTFOLDER". code is given below, every time it is going to the catch section. how to open it?

 var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/TESTFOLDER";
                Java.IO.File file = new Java.IO.File(externalPath, "qdummy.pdf");
                if(file.Exists())
                {
                    try
                    {
                        file.SetReadable(true);
                        Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
                        Intent intent = new Intent(Intent.ActionView);
                        intent.SetDataAndType(uri, "application/pdf");
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);                        
                        intent.SetFlags(ActivityFlags.NoHistory);
                        StartActivity(intent);                        
                    }
                    catch (Exception ex)
                    {
                        Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
                    }
                }
                else
                {
                    Toast.MakeText(Application.Context, "No exx", ToastLength.Long).Show();
                }

Best Answer

  • soumenhaldersoumenhalder Member ✭✭✭
    Accepted Answer

    got the solution

    try
                        {
                            string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                            string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                            Intent intent = new Intent(Intent.ActionView);
                            intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
                            Android.Net.Uri path = FileProvider.GetUriForFile(Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
                            intent.SetDataAndType(path, mimeType);
                            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                            Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
                        }
                        catch (Exception ex)
                        {
                            Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
                        }
    

Answers

  • XamarinProblemsXamarinProblems Member ✭✭✭

    On android you can't open a file from the internal storage directly, you have to find the file, then copy the filestream and save it to a temporary storage location, you can then use the path to the file in a custom WebView.

    Check:
    https://github.com/xamarin/docs-archive/tree/master/Recipes/xamarin-forms/Controls/display-pdf

  • soumenhaldersoumenhalder Member ✭✭✭
    Accepted Answer

    got the solution

    try
                        {
                            string extension = MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                            string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                            Intent intent = new Intent(Intent.ActionView);
                            intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
                            Android.Net.Uri path = FileProvider.GetUriForFile(Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
                            intent.SetDataAndType(path, mimeType);
                            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                            Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
                        }
                        catch (Exception ex)
                        {
                            Toast.MakeText(Application.Context, "problem", ToastLength.Long).Show();
                        }
    
Sign In or Register to comment.