简体   繁体   English

无法在 Android 中打开 pdf

[英]Unable to open the pdf in Android

I am new in android and working on one android project in which I have to display a chosen pdf from the device either from internal storage(Priority) or from external Storage.我是 android 新手,正在开发一个 android 项目,在该项目中,我必须从内部存储(优先级)或外部存储显示设备中选择的 pdf。 I am enclosing the used code below.我在下面附上了使用过的代码。

private void openLocalPDF(File pdffile) {

       File file = new File(Environment.getExternalStorageDirectory(), pdffile.getName());
        Uri path = PdfFileProvider.getUriForFile(activity.getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
        Intent target = new Intent(Intent.ACTION_VIEW);
        target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        target.setDataAndType(path, "application/pdf");
        Intent intent = Intent.createChooser(target, "Open File");
        try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "Please install some pdf viewer app", Toast.LENGTH_LONG).show();
            }

You should create a Provider Class and extends with FileProvider .您应该创建一个Provider Class并使用FileProvider extends and register in manifest and also if you using targetSdkVersion 29 add this permission AndroidManifest.xml android:requestLegacyExternalStorage="true"并在manifest注册,如果您使用targetSdkVersion 29添加此权限AndroidManifest.xml android:requestLegacyExternalStorage="true"

<provider
        android:authorities="androidx.core.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true"
        android:name=".provider.GenericFileProvider">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>

    </provider>

And add a provider_paths.xml file in xml folder :并在xml文件夹中添加一个provider_paths.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

And use this method并使用这种方法

public static void openFile(Context context, File file) {
    Uri path = GenericFileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(path, "application/pdf);
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {

    }
}

I hope this will help you.我希望这能帮到您。

Follow below steps:请按照以下步骤操作:

Step - 1: Create provider_paths.xml in your xml directory步骤 - 1:在您的xml目录中创建provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

Step - 2: Add FileProvider in your AndroidManifest.xml file步骤 - 2:在您的AndroidManifest.xml文件中添加FileProvider

<provider
    android:name="androidx.core.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>

Step - 3: Since your file is in internal/external storage use getExternalStorageDirectory()步骤 - 3:由于您的文件在内部/外部存储中,请使用getExternalStorageDirectory()

File file =  new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);

Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);

Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(path, "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(getActivity(), "Please install some pdf viewer app", Toast.LENGTH_LONG).show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM