Почему FileProvider открывает пустые или поврежденные файлы? Я перепробовал все решения, приведенные в других подобных вопросах - PullRequest
0 голосов
/ 31 августа 2018

Я пытаюсь создать приложение для создания ярлыков файлов на домашнем экране. У меня есть путь к файлу, и я пытаюсь открыть файл с помощью FileProvider. Он открывает правильный файл каждый раз, но нет содержимого, только пустой экран. Аналогичная ситуация возникает с изображениями и другими файлами. Вот мой код:

MainActivity.java

...
private void ShortcutIcon(String path){

    File file = new File(path);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID,file) ;
    intent.setDataAndType(uri,"application/pdf");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);                      //simply to check if it's working

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_chooser));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

AndroidManifest.xml

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml

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

Полный код можно найти здесь . Любая помощь будет оценена.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...