Как создать PDF Viewer для Android на C # с предварительным просмотром - PullRequest
0 голосов
/ 06 февраля 2019

Я хочу создать или найти приложение, которое читает файл PDF на Android с возможностью предварительного просмотра.Таким образом, он помещает миниатюры всех страниц на 1.

Как я могу сделать это на Xamarin с C #?

1 Ответ

0 голосов
/ 07 февраля 2019

Пожалуйста, следуйте этим шагам.

Шаг 1: Необходимо добавить провайдера в AndroidManifest.xml под тегом приложения

<application>
    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="your package name.provider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_paths" />
          </provider>
     </application>

Шаг 2: Открыть файл в программе просмотра PDF из папки Assets

  private void OpenPdfInExternalApplication(string filename)
    {
        AssetManager assetManager = Activity.Assets;
        string fileName1 = filename + ".pdf";
        var file = new File(Activity.FilesDir, fileName1);

        Uri uri = null;
        Intent intent = new Intent(Intent.ActionView);
        try
        {
            // Get the input stream
            Stream input = assetManager.Open(fileName1);
            Stream output;
            if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
              {
                output = Activity.OpenFileOutput(file.Name, FileCreationMode.Private);
                uri = FileProvider.GetUriForFile(Activity, Activity.PackageName + ".provider", file);
                 intent.SetDataAndType(uri, "application/pdf");
              }
              else
              {
                   output = Activity.OpenFileOutput(file.Name, FileCreationMode.WorldReadable);
                  intent.SetDataAndType(Uri.Parse("file://" + Activity.FilesDir + "/" + fileName),
                 "application/pdf");
              }
            input.CopyTo(output);
            intent.AddFlags(ActivityFlags.GrantPrefixUriPermission);
            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        try
        {
            StartActivity(intent);
        }
        catch (ActivityNotFoundException ex)
        {
            Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
        }
    }
...