Как открыть файл PDF из внутреннего хранилища в Oreo? - PullRequest
0 голосов
/ 13 октября 2018

Я пытаюсь открыть PDF-файл в Oreo, но он не открывается.Я не получаю никакой ошибки.В чем проблема?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();
        }

        }

В Манифест Я также добавляю

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

1 Ответ

0 голосов
/ 13 октября 2018

Замените значение вашего пути на "."- Посмотрите на ниже:

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

Внутри манифеста, код «провайдера» такой, ваш тоже верный.

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

Надеюсь, это поможет вам.

...