Forum Xamarin.Forms

Intent doesn't find the file

I have a class to open Pdf and office document (Word, Excel, Powerpoint) with intent for Android.

Everything is fine with Pdf but it doesn't work with office documents. It does not seem to find the file.

Can you help me?

Thanks!

public class PdfWebViewDroid : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var customWebView = Element as PdfWebView;
            Control.Settings.AllowUniversalAccessFromFileURLs = true;

            var extension = System.IO.Path.GetExtension(customWebView.Uri);

            if (extension == ".pdf")
            {
                Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}",
                    string.Format("file:///{0}", WebUtility.UrlEncode(customWebView.Uri))));
            }
            else
            {
                File targetFile = new File(customWebView.Uri);
                Uri targetUri = Uri.FromFile(targetFile);
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(targetUri, "application/*");
                intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                Context.StartActivity(intent);
            }
        }
    }
}
Tagged:

Best Answer

  • Jean-FrancoisBouchard.2645Jean-FrancoisBouchard.2645 USMember ✭✭
    Accepted Answer

    I resolved my problem by putting the temp file in external storage instead of Environment.SpecialFolder.Personal :

    It seems that intent cannot access Environment.SpecialFolder.Personal.

    Changes in my code (some class) :

    //var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    //var filePath = Path.Combine(documentsPath, filename);
    //File.WriteAllBytes(filePath, bytes);

    var documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
    var filePath = Path.Combine(documentsPath.Path, filename);
    File.WriteAllBytes(filePath, bytes);

Answers

  • Maybe i need something special to access this folder : file:////data/user/0/com.my.app/files/filetemp.docx ???

    Help me!!!! ;-)

  • Jean-FrancoisBouchard.2645Jean-FrancoisBouchard.2645 USMember ✭✭
    Accepted Answer

    I resolved my problem by putting the temp file in external storage instead of Environment.SpecialFolder.Personal :

    It seems that intent cannot access Environment.SpecialFolder.Personal.

    Changes in my code (some class) :

    //var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    //var filePath = Path.Combine(documentsPath, filename);
    //File.WriteAllBytes(filePath, bytes);

    var documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
    var filePath = Path.Combine(documentsPath.Path, filename);
    File.WriteAllBytes(filePath, bytes);

Sign In or Register to comment.