Открытие PDF-файла в Acrobat Reader - PullRequest
0 голосов
/ 13 марта 2020

В моем приложении я хотел бы показать пользователю файл справки (PDF). Файл справки публикуется как ресурс и по первому запросу копируется во внешнее хранилище. Adobe Acrobat Reader запускается, но не показывает файл .

Что мне не хватает?

Я использую пакет платформы 21 и Acrobat Reader 20.1.1 на Android 9.1 .

Примечание : с помощью средства просмотра файлов я вижу файл и при касании Adobe Acrobat Reader открывается вместе с файлом. Adobe Acrobat Reader установлен как приложение по умолчанию с опцией «Всегда».

Вот код:

public static void open(Activity activity)
{
    // copy help file to external storage
    String filename = "helpfile.pdf";
    File pubFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename);
    if(!pubFile.exists())
    {
        InputStream ims = null;
        try {
            ims = activity.getAssets().open(filename);
            Files.copy(ims, pubFile.toPath(),StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            if(ims != null)
            {
                try {
                    ims.close();
                    pubFile.delete();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    Toast.makeText(activity, "Failed to copy help file to accessible folder.", Toast.LENGTH_SHORT);
                    return;
                }
            }
            e.printStackTrace();
            Toast.makeText(activity, "Failed to copy help file to accessible folder.", Toast.LENGTH_SHORT);
            return;
        }
    }

    // open the help file with application for PDF files (Adobe Acrobat)
    if(pubFile.exists() && pubFile.canRead())
    {
        // EDIT Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(pubFile));
        Intent intent = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(activity
                        , "com.myapp.fileprovider"
                        , pubFile)); // EDIT
        intent.setType("application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        PackageManager pm = activity.getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        if (activities.size() > 0) {
            activity.startActivity(intent);
        } else {
            Toast.makeText(activity, "No application to show help file.", Toast.LENGTH_SHORT);
        }
    }
}

РЕДАКТИРОВАТЬ

Вот что Я сделал по рекомендации CommonsWare

  1. Заменил Uri.fromFile (...) на FileProvider.getUriForFile (...) (см. Код выше)

  2. AndroidManifest. xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp" android:versionCode="1" android:versionName="1.0">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.myapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</manifest>
src \ android \ res \ xml \ file_paths. xml:
<paths>
    <external-path name="external" path="." />
</paths>
...