android - 如何在 Oreo 中打开内部存储中的 PDF 文件?

标签 android android-8.0-oreo

我尝试在 Oreo 中打开 PDF 文件,但它打不开。我没有收到任何错误。问题是什么? PDF 文件打不开。仅显示黑屏。在 logcat 中没有错误显示。怎么了?

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

我的代码是:

   File file11 = new File(Environment.getExternalStorageDirectory().
   getAbsolutePath(), 
   "AtmiyaImages/" + nameoffile1);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(Build.VERSION.SDK_INT>24){
        Uri uri=FileProvider.getUriForFile(AssignmentActivity.this,
        getPackageName()+".provider",file11);
        target.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        target.putExtra(Intent.EXTRA_STREAM,uri);
        target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        target.setType("application/pdf");
        Intent intent=Intent.createChooser(target,"Open File");
        try
        {
        startActivity(intent);
        }
        catch(ActivityNotFoundException e)
        {
        Toast.makeText(AssignmentActivity.this,"No Apps 
       can performs This acttion",Toast.LENGTH_LONG).show();
        }
        }

        else
        {
        target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        target.setDataAndType(Uri.fromFile(file11),"application/pdf");
        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        Intent intent=Intent.createChooser(target,"Open File");
        try
        {
        startActivity(intent);
        }
        catch(ActivityNotFoundException e)
        {
        Toast.makeText(AssignmentActivity.this,"No Apps can performs This 
        acttion",Toast.LENGTH_LONG).show();
        }

        }

Manifest中我也添加

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.infinity.infoway.atmiya.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
       </provider>

我的Xml代码是:

 <?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
 name="Phonestorage"
path="/storage/emulated/0/AtmiyaImages"/>
</paths>

最佳答案

将您的路径值替换为“.” - 看下面:

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

在Manifest里面,“provider”代码是这样的,你的也是。

<provider
            android:name="android.support.v4.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>

要访问正确的文件路径,请遵循以下代码:

File  pdfFile = new File(getExternalFilesDir(GlobalUtils.AppFolder), fileName);
     Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", pdfFile);
                Log.e("create pdf uri path==>", "" + path);

                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(path, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(intent);
                    finish();
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getApplicationContext(),
                            "There is no any PDF Viewer",
                            Toast.LENGTH_SHORT).show();
                    finish();
                }

希望对您有所帮助。

关于android - 如何在 Oreo 中打开内部存储中的 PDF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789578/

相关文章:

java - 如何删除 ListView 的蓝色边缘滚动线?

android - 在安卓8.0以上。有什么办法可以知道系统中是否开启了 pip 模式

android - 通知 channel 错误

android 使用 START_STICKY 启动前台服务

android - 根据当前位置和某个位置之间的距离更改 LocationRequest 的间隔

android - 删除动态创建的 ImageViews Android 之间的空白

android - Mac 上 Android Studio(和其他应用程序)的选项卡

android - 在 Oreo 中禁用状态栏拉取

android - 电池优化对话框在 Android Oreo 中不起作用

android - 如何使用命令行在 Android Studio 之外安装 android 约束布局工具?