Возникли проблемы с отображением файла PDF в моем приложении - PullRequest
0 голосов
/ 23 мая 2018

Мне удалось успешно загрузить файл PDF с сервера (веб-API), используя следующий код

    HttpURLConnection connection = null;
    File file;
    File outputFile = null;
    FileOutputStream fileOutputStream = null;
    InputStream inputStream = null;
    int count;
    string filename = "xxx.pdf"
    try {
        //output path would be "/data/user/0/package.ofapplication/files/folder/"
        file = new File(interface.getFileDirectory(),"folder"); 

        if (!file.isDirectory()) file.mkdirs();

        //output would be "/data/user/0/package.ofapplication/files/folder/xxx.pdf"
        outputFile = new File(file, filename);

        URL url = new URL(getUrlMethod());
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        int fileLength = connection.getContentLength();

        inputStream = new BufferedInputStream(url.openStream());

        fileOutputStream = new FileOutputStream(outputFile);

        .......other
        .......related
        .......downloading codes

        //this would return "/data/user/0/package.ofapplication/files/folder/xxx.pdf"
        return outputFile.getPath();
    }

Теперь я получаю сообщение об ошибке при попытке просмотреть файл PDF с помощьюследующий код в Android.

// output path would be /data/user/0/package.ofapplication/files/folder/
File pdfFile = new File(pdfPath);

Intent pdfIntent = new Intent(Intent.ACTION_VIEW)
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

//this is the part where im getting an error
pdfIntent.setDataAndType(FileProvider.getUriForFile(this.getActivity(), this.getActivity().getApplicationContext().getPackageName() + ".provider",pdfFile), "application/pdf");

Сообщение об ошибке, полученное из приведенного выше кода, выглядит следующим образом:

java.lang.IllegalArgumentException: не удалось найти настроенный корень, содержащий /data / data / package.ofapplication / files / folder / 1.pdf

Может кто-нибудь, пожалуйста, помогите мне в этом, я плачу и ищу часы прямо сейчас и до сих пор не могу найти способ.Я также пробовал эту ссылку, но у меня по-прежнему та же ошибка

FileProvider - IllegalArgumentException: не удалось найти настроенный root

Примечание: мойпуть_поставщика ниже

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

1 Ответ

0 голосов
/ 23 мая 2018

Похоже, вы пытаетесь сохранить файл в internal storage и определяете элемент пути в XML как external storage как <external-path, поэтому попробуйте изменить элемент пути в XML как internal storage:

<external-path

TO

<files-path

Подробнее.https://developer.android.com/reference/android/support/v4/content/FileProvider

...