Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

unable to open pdf file on android 10 using xamarin forms

$
0
0

I have a developed code to save and open a pdf file and used to work on all android devices. Recently I figured it is not working on android 10.

Code to Save file:

    public async void SaveFile(byte[] bytes, string folderPath, string fileName)
    {
            if (IsFileExists(folderPath, fileName))
            {
                DeleteFile(folderPath, fileName);
            }
            string path = GetFilePath(folderPath, fileName); //path = "/storage/emulated/0/3080/TCPS/584"
            Directory.CreateDirectory(path);
            path = System.IO.Path.Combine(path, fileName); //filename = "Fugro TCO B-17-009.pdf"
            System.IO.File.WriteAllBytes(path, bytes);
        }
    } 

To Open the file:

public void OpenFile(string folderPath, string fileName)
    {
        MimeTypeMap mimeTypeMap = MimeTypeMap.Singleton;
        Intent newIntent = new Intent(Intent.ActionView);
        string path = GetFilePath(folderPath, fileName);
        string mimeType = null;
        String extension = MimeTypeMap.GetFileExtensionFromUrl(fileName);
        if (extension != null)
        {
            mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension.ToLower());
        }
        newIntent.SetDataAndType(Android.Net.Uri.FromFile(GetFile(path, fileName)), mimeType);
        newIntent.SetFlags(ActivityFlags.NewTask);
        Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity.StartActivity(newIntent);
    }

Here, if the file is pdf, I'm getting mime-type as null but it's work fine on android 9 and below. Can anyone explain what needs to be done to get it to work on android 10?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>