0

I am facing below error when trying to launch a PDF file stored in assets folder of android project in xamarin forms.

"this file could not be accessed. check your connection or make the filename shorter in xamarin forms"

Giving shorter file name didn't help.

Below is my code:

public interface IDocumentView
    {
        void ShowPDFTXTFromLocal(string filename);
    }


[assembly: Xamarin.Forms.Dependency(typeof(DocumentView))]
namespace Portfolio_Pdf.Droid.Platform
{
    public class DocumentView : IDocumentView
    {
        [Obsolete]
        public void ShowPDFTXTFromLocal(string filename)
        {
            string reportSavedPath = "/data/user/0/com.companyname.portfolio_pdf/files/test.pdf";
            Java.IO.File file = new Java.IO.File(reportSavedPath);

            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                var fileUri = Android.Net.Uri.FromFile(new Java.IO.File(reportSavedPath));
                Intent intent = new Intent(Intent.ActionView);
                var mimetype = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl((string)fileUri).ToLower());
                Android.Net.Uri apkURI = FileProvider.GetUriForFile(
                                 Xamarin.Forms.Forms.Context.ApplicationContext,
                                 Xamarin.Forms.Forms.Context.ApplicationContext.PackageName + ".provider", file);
                intent.SetDataAndType(apkURI, mimetype);
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                intent.SetFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.ClearTop);
                intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.ClearTop);

                try
                {              
                    Xamarin.Forms.Forms.Context.StartActivity(intent);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }

            });   
        }
    }
}

Android.Manifest.xml:

<provider
             android:name="android.support.v4.content.FileProvider"
             android:authorities="${applicationId}.provider"
             android:exported="false"
             android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
      </provider>

provider_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path
    name="external"
    path="." />
  <external-files-path
    name="external_files"
    path="." />
  <cache-path
    name="cache"
    path="." />
  <external-cache-path
    name="external_cache"
    path="." />
  <files-path
      name="files"
      path="." />
</paths>
Mars
  • 269
  • 1
  • 3
  • 22
  • "stored in assets folder" - is it actually an Android Asset? Or a file you downloaded at runtime? – Jason Oct 14 '21 at 15:42
  • 1
    I think that if you use [Xamarin.Essentials.Launcher](https://learn.microsoft.com/en-us/xamarin/essentials/launcher?context=xamarin%2Fandroid&tabs=android#files) in your shared code with a pdf file, it will detect if you have an application to open it, and then do it – Juan Sturla Oct 14 '21 at 15:50
  • @Jason, It's an Android Asset – Mars Oct 14 '21 at 15:51
  • 3
    Assets are NOT files - they need to be accessed with AssetManager, not with normal File IO. See https://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder – Jason Oct 14 '21 at 15:53

0 Answers0