如何打开Oreo内部存储器中的PDF文件? [英] How to open PDF file from internal storage in Oreo?

查看:79
本文介绍了如何打开Oreo内部存储器中的PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to open PDF file in Oreo but it does not open. I don't get any error. What is the issue? PDF file does not open. Only black screen is shown. In logcat no errors show. What is wrong?

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

How Can I resolve this issue? I referred many links but did not get solution. I also tried many codes but no help.

我的代码是:

   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();
        }

        }

清单中,我还添加

   <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>

推荐答案

将路径值替换为". -看下面:

Replace your path value with "." - Look at below:

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

在清单中,提供程序"代码是这样的,您的代码也是如此.

Inside Manifest, "provider" code is like this, your one is also true.

<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>

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

For access proper file path follow this code:

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();
                }

希望这会对您有所帮助.

Hope this will help you.

这篇关于如何打开Oreo内部存储器中的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆