如何从Pie的内部存储打开PDF文件?

时间:2019-09-14 06:49:00

标签: android android-9.0-pie

我尝试在Pie中打开PDF文件吗?但它无法打开。我没有任何错误。有什么问题PDF文件无法打开。仅显示黑屏。在logcat中没有错误显示。怎么了?

Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri outputFileUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            outputFileUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
        } else
            outputFileUri = Uri.fromFile(file);

        Log.e("TAG", "outputFileUri-> " + outputFileUri);
        intent.setDataAndType(outputFileUri, "application/pdf");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
        }


AndroidManifest.xml
<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>

<paths>
<external-path
    name="external_files"
    path="." />
<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="." />

如何解决此问题?我提到了许多链接,但没有得到解决方案。我也尝试了很多代码,但没有帮助。

1 个答案:

答案 0 :(得分:0)

File dwldsPath = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "test.pdf"));`enter code here`
                    byte[] pdfAsBytes = Base64.decode(content, 0);
               `enter code here`     FileOutputStream os;
                    os = new FileOutputStream(dwldsPath, false);
                    os.write(pdfAsBytes);
                    os.flush();
                    os.close();
                    if (dwldsPath.exists()) {
                        Uri path = Uri.fromFile(dwldsPath);
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(path, "application/pdf");
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        // If requesting from a fragment...
                        this.startActivity(intent);
                        return;
                    }