Как FileProvider выбирает / различает пути провайдера в provider_path.xml? - PullRequest
0 голосов
/ 10 октября 2018

Мне нужно использовать FileProvider для установки apk.Теперь все работает хорошо, но я не совсем понимаю, как следует FileProvider, чтобы выбрать правильный путь, определенный внутри provider_paths.xml .Вот мой код:

Manifest.xml

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

main_activity.java

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        intent = new Intent(Intent.ACTION_VIEW);
        File apk = new File("/sdcard/example.apk");
        String fileType = "application/vnd.android.package-archive";
        intent.setDataAndType(Uri.fromFile(apk), fileType);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    } else {
        intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        File directory = new File(Environment.getExternalStorageDirectory().toString());
        File apk = new File(directory, "example.apk");
        Uri fileUri = FileProvider.getUriForFile(this, valoresGenerales.appContext.getPackageName() + ".provider", apk);
        String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apk.getName()));
        intent.setDataAndType(fileUri, type);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }

    startActivity(intent);

provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
    <files-path name="files_path"  path="Download"/>
</paths>

От чего зависит выбор FileProvider между external_path или files_path (или что-либо определенное в файле)?

...