嗨stackoverflow我真的很难打开安卓nougat上下载的apk。我知道需要使用文件提供程序,我只是不知道如何输入正确的路径。应用程序崩溃或找不到文件。它总是打开/storage/emulated/0/android/data/cf.vojtech.apkmirror/downloads,当然这是不存在的。我需要应用程序访问内部存储中的下载目录(类似environment.getexternalstoragedirectory().getpath()+“/”+environment.directory\u downloads+“/”+filename)
有什么想法吗?我已经在网上找了一个多小时了,但就是找不到(我当时非常生气,想把飞机坠毁的事留在那儿,不理它)
main活动.java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        File apkfile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName);
                        Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "cf.vojtechh.apkmirror.provider", apkfile);
                        String s = apkUri.toString();
                        Log.d("HERE!!",s);
                        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                        intent.setData(apkUri);
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivity(intent);

} else {
                        File apkfile = new File(Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + fileName);
                        Uri apkUri = Uri.fromFile(apkfile);
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
}

提供程序路径.xml
    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path name="Download" path="Download/"/>
    </paths>

androidmanifest.xml文件
<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="cf.vojtechh.apkmirror.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

最佳答案

尝试改用Environment#getExternalStoragePublicDirectory()方法。Context#getExternalFilesDir()用于应用程序专用的文件,因此包含应用程序包名称的路径(技术上,此目录是公共的,但仍然不是您要查找的目录)。

File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

然后可以创建下载文件的File表示:
File apk = new File(downloads, filename);

10-07 19:47
查看更多