Forum Xamarin.Android

How to open PDF in external app?

pickwickpickwick PLMember
edited August 2015 in Xamarin.Android

I have problem with opening saved PDF file in external app. When I try to run a code below, I get error from Adobe Acrobat about incorrect path.
I guess the problem is that the file exist in '/data/data/[app.name]..' and Adobe Acrobat can't access it. What should I do to solve this problem?
Here's how I save and try to open file.
public async Task OpenFile(string name) { File file = new File(Environment.GetFolderPath(Environment.SpecialFolder.Personal), name); Intent intentPdf = new Intent(Intent.ActionView); intentPdf.SetDataAndType(Uri.FromFile(pdfFile), "application/pdf"); Forms.Context.StartActivity(intent); } public async void SavePDF(byte[] pdf, string name) { File file = new File(Environment.GetFolderPath(Environment.SpecialFolder.Personal), name); if (file.Exists()) { file.Delete(); } try { using (var outs = new FileOutputStream(file)) { await outs.WriteAsync(pdf); } } catch (Exception e) { Toast.MakeText(Forms.Context, e.Message, ToastLength.Short).Show(); } }

Tagged:

Posts

  • manoharbonalamanoharbonala INMember ✭✭

    intentPdf.SetDataAndType(Uri.FromFile(pdfFile), "application/pdf");

    in the above line you are passing "pdfFile" instead of "file".so you are getting the errror.
    try the below code

    File file = new File(Environment.GetFolderPath(Environment.SpecialFolder.Personal), name);
    Intent intentPdf = new Intent(Intent.ActionView);
    intentPdf.SetDataAndType(Uri.FromFile(file), "application/pdf");
    Forms.Context.StartActivity(intent);

  • pickwickpickwick PLMember

    It isn't the cause. I'm passing correct ('pdf') value in my code, it's just a typo in that snippet.

  • manoharbonalamanoharbonala INMember ✭✭

    @pickwick first copy the pdf to external directory then you try to open it.use the following code

    string fileName=new File(Environment.GetFolderPath(Environment.SpecialFolder.Personal), name);
    String destPath = Android.OS.Environment.ExternalStorageDirectory.Path + "your own directory(destination)";
    destPath = destPath + "/Sample.pdf";
    File.Copy(fileName, destPath, true);

    Java.IO.File file = new Java.IO.File(destPath);
    Intent intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
    StartActivity(intent);

  • pickwickpickwick PLMember

    @manoharbonala
    Thanks it works. But is it correct way to open files in external apps by coping to external storage? I'm new to android dev and I don't know good practices.

  • leninmathileninmathi USMember ✭✭

    Hi,

    i'm getting the error as "ActivityNotFoundException: No Activity found to handle Intent" at the startActivity(intent) line...

Sign In or Register to comment.