代码之家  ›  专栏  ›  技术社区  ›  dinhlam

如何在Android 8.1中按供应商从存储器中打开文件

  •  0
  • dinhlam  · 技术社区  · 6 年前

    我想从存储器中打开并安装一个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);
            }
    

    在我的 显示 文件:

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

    我的 提供程序路径.xml :

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

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Dhruv Patel    6 年前

    要请求安装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>
    

    提供商路径应该是(它将覆盖几乎所有可用的设备),

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