【问题标题】:intent to install apk on android N打算在android N上安装apk
【发布时间】:2018-06-06 10:52:59
【问题描述】:

我正在尝试在 android N 上安装 APK。 我使用以下代码:

File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    intent.setData(apkUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    activity.startActivity(intent)
} else {
    Uri apkUri = Uri.fromFile(toInstall);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivity(intent);
}

但我遇到以下错误:

    FATAL EXCEPTION: Thread-5
Process: com.myapp.myapp, PID: 16557
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at ir.myapp.picwriter.service.MyService$1.run(MyService.java:57)
at java.lang.Thread.run(Thread.java:761)

这一行的错误是什么:

Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);

【问题讨论】:

  • IIRC,该错误意味着 Android 无法在清单中使用您指定的权限字符串找到您的 FileProvider
  • @CommonsWare 那我该怎么办?
  • 由于我看不到您的清单或您的 FileProvider 元数据 XML 资源,我真的无法给您更多建议。如果您编辑您的问题并将其添加到其中,也许我们可以指出要更改的内容。
  • @CommonsWare 我的清单中没有FileProvider 或任何FileProvider 我只是从stackoverflow.com/a/40131196/7297151 复制代码并搜索错误但我无法得到任何答案所以我问了一个问题.

标签: java android android-intent


【解决方案1】:

我遇到了很多问题,最后我找到了答案。这是答案希望它对你有用。

    File toInstall = new File(url);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        File newFile = new File(new File(Environment.getExternalStorageDirectory(), "apps"), getImageNameByUrl(appUrl));
        Uri apkUri = getUriForFile(context, BuildConfig.APPLICATION_ID+".provider", newFile);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent);
    } else {
        Uri apkUri = Uri.fromFile(toInstall);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

manifest:

    <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/file_paths" />
    </provider>

file_paths:

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

API 权限>25

<uses-permissionandroid:name="android.permission.REQUEST_INSTALL_PACKAGES" />

【讨论】:

  • 另外,如果目标 API > 25,记得添加权限android.permission.REQUEST_INSTALL_PACKAGES。否则什么都不会发生。
【解决方案2】:

如果在清单文件中找不到文件提供程序,您必须执行以下操作来定义它:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    ...
</provider>

这里是有关设置 FileProviders 的文档的链接。 https://developer.android.com/reference/android/support/v4/content/FileProvider.html

【讨论】:

  • 好吧,我没有任何名为fileprovider 的类,这有什么问题吗?什么是com.mydomain?应该用什么代替... 谢谢。
  • 权限只是您将包设置为的包名称:com.myapp.myapp。这意味着您将替换为权威。
  • 而且名字不一定非得叫fileprovider,什么都可以叫
  • 我会阅读答案中的链接,它会告诉您有关 FileProvider 的所有信息
  • @sajadabasi 如果对您有帮助,您能接受我的回答吗?
猜你喜欢
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2017-02-14
  • 2017-11-17
  • 1970-01-01
  • 2017-03-06
  • 2019-05-06
  • 1970-01-01
相关资源
最近更新 更多