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

Unable to Open PDF into the third-party app

$
0
0

Hi,

I am downloading file into download folder. A pdf file is downloaded properly but unable to open into third-party app.

  1. If I select Target SDK : Automatic into the droid then it works perfectly. I am able to show pdf file with the third-party app. But with this selection, I can't publish app on the play store. It throws an error Target SDK should be 23 or greater.

  2. If I select Target SDK other than automatic then it's not allowing to open PDF file into the third-party app.
    Please see my code below.

public void OpenFile(string filePath)
{
string externalStorageState = global::Android.OS.Environment.ExternalStorageState;
string application = "";

        string extension = System.IO.Path.GetExtension(filePath);

        switch (extension.ToLower())
        {
            case ".doc":
            case ".docx":
                application = "application/msword";
                break;
            case ".pdf":
                application = "application/pdf";
                break;
            case ".xls":
            case ".xlsx":
                application = "application/vnd.ms-excel";
                break;
            case ".jpg":
                application = "image/jpeg";
                break;
            case ".jpeg":
                application = "image/jpeg";
                break;
            case ".png":
                application = "image/png";
                break;
            default:
                application = "*/*";
                break;
        }
        //var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/report" + extension;
        //File.WriteAllBytes(externalPath, bytes);

        //Java.IO.File file = new Java.IO.File(externalPath);
        Java.IO.File file = new Java.IO.File(filePath);
        file.SetReadable(true);
        //Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + filePath);
        Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, application);
        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

        try
        {
            Xamarin.Forms.Forms.Context.StartActivity(intent);
        }
        catch (Exception)
        {
            Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View this file.", ToastLength.Short).Show();
        }
    }

The code is crashing on this line Xamarin.Forms.Forms.Context.StartActivity(intent);

Please help me.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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