【问题标题】:How to open file from storage by Provider in Android 8.1如何在 Android 8.1 中通过 Provider 从存储中打开文件
【发布时间】:2019-01-23 09:33:12
【问题描述】:

我想从存储中打开并安装一个 apk 文件,我使用了以下代码:

        File fileToOpen = new File(path);
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String ext = path.substring(path.lastIndexOf(".") + 1);
        String type = mime.getMimeTypeFromExtension(ext);

        if (fileToOpen.exists()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", fileToOpen);
                intent.setDataAndType(uri, type);
            } else {
                intent.setDataAndType(Uri.fromFile(fileToOpen), type);
            }
            context.startActivity(intent);
        }

在我的 ma​​nifest 文件中:

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

我的 provider_path.xml

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

我必须在运行时请求权限并接受,但在我调用此方法后,什么也没有发生。文件已经存在,我的代码中发生了什么?请帮我找到它,非常感谢:((

【问题讨论】:

    标签: android android-fileprovider


    【解决方案1】:

    要请求安装 APK,

    File file = new File("YOUR_FILE_PATH");
    
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
    } else {
        uri = Uri.fromFile(file);
    }
    
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    startActivityForResult(intent);
    

    你的清单应该是,

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

    provider_path 应该是(它将覆盖几乎所有可用的设备),

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
            name="external_files"
            path="." />
        <root-path
            name="external_files"
            path="/storage/" />
    </paths>
    

    【讨论】:

    • 我想你忘了在 manifeast.xml 中添加你的包名。如果仍然存在,请尝试更改 provider_path.xml。
    • 非常感谢你,我会试试的
    • 我试过了,但一切都一样,我找到了一个解决方案,即在清单中添加 REQUEST_INSTALL_PACKAGE,它正在工作
    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-07-07
    • 2017-04-25
    • 2018-10-13
    相关资源
    最近更新 更多