用于打开pdf文件的Android intent文件管理器

用于打开pdf文件的Android intent文件管理器,android,intentfilter,Android,Intentfilter,我添加了我的pdf查看器应用程序,以使用pdf文件列表完成操作。但是如果我运行列表上的文件,应用程序就会关闭 我在onCreate方法和manifest文件中创建了意图过滤器,但我不知道如何修复此错误 PdfActivity.java AndroidManifest.xml 请帮我解决这个问题。试试这个 //Method to generate a MIME Type private static String getMimeType(String url) { String t

我添加了我的pdf查看器应用程序,以使用pdf文件列表完成操作。但是如果我运行列表上的文件,应用程序就会关闭

我在onCreate方法和manifest文件中创建了意图过滤器,但我不知道如何修复此错误

PdfActivity.java

AndroidManifest.xml

请帮我解决这个问题。

试试这个

  //Method to generate a MIME Type
  private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

 //Method to generate File URI
 private static Uri getFileUri(String filePath) {
    Uri fileUri = null;
    File file = new File(filePath);
    try {
        if (Build.VERSION.SDK_INT >= 24) {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }
        fileUri = Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileUri;
}


 //Intent
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
 startActivity(intent);

检查一下,谢谢。将意图行放在onCreate方法中是否正确?我想把应用程序链接到每个存储中的pdf文件,而不是特定的资源文件夹。如何修复setDataAndType方法中的文件路径?
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".PdfActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:scheme="file" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
    </activity>

</application>
  //Method to generate a MIME Type
  private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

 //Method to generate File URI
 private static Uri getFileUri(String filePath) {
    Uri fileUri = null;
    File file = new File(filePath);
    try {
        if (Build.VERSION.SDK_INT >= 24) {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }
        fileUri = Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileUri;
}


 //Intent
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
 startActivity(intent);