Hi,
I am downloading file into download folder. A pdf file is downloaded properly but unable to open into third-party app.
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.
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.