【问题标题】:getting exposed beyond app through Intent.getData() error while installing the downloaded apk file在安装下载的 apk 文件时通过 Intent.getData() 错误暴露在应用程序之外
【发布时间】:2018-08-21 05:52:59
【问题描述】:

我已经从服务器下载了 apk 文件,现在正在安装它,但它给了我一个错误:通过 Intent.getData() 暴露在应用之外

我的代码是

Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);


        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
                "/download/" + "in.sample.myapp.apk")), "application/vnd.android.package-archive");

我已经搜索了错误并找到了一些但无法解决问题。建议是使用 FileProvider。我这样做只是为了检查日志

Log.d("NewPath", "NewPathName is: " +(FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",outputFile)));

可以帮助解决这个问题。

请帮助和建议。

【问题讨论】:

  • 共享两种情况的日志
  • 您需要将Intent 上的Uri.fromFile() 替换为从FileProvider.getUriForFile() 返回的Uri。只是在某个任意位置调用FileProvider.getUriForFile() 并不能修复该异常。
  • @MikeM。谢谢,我已经替换了下面的代码 'intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "in.sample.myapp.apk")), "application/vnd.android.包存档");'用'intent.setDataAndType(FileProvider.getUriForFile(context,BuildConfig.APPLICATION_ID + ".provider",outputFile), "application/vnd.android.package-archive");'错误日志'尝试在空对象引用上调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)'跨度>
  • 您是否完全设置了FileProvider?它不仅仅是调用getUriForFile() 方法。 developer.android.com/reference/android/support/v4/content/…

标签: android


【解决方案1】:

你的代码

Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory() + "/FolderName/" + "yourFile.apk");
Uri data = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID +".provider",file);
intent.setDataAndType(data,"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);

文件路径.xml

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

清单

<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/filepaths"/>
    </provider>
</application>

【讨论】:

  • 工作就像一个魅力(在 API 版本 24 上测试),谢谢。
【解决方案2】:

使用 FileProvider,示例代码如下:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider" ,file);
    intent.setDataAndType(data, "/*");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(intent);

您还需要在清单文件中包含 FileProvider 在应用程序标签中这样做:

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

同样在资源目录下创建一个xml文件夹,并创建一个文件provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path name="PATH here" path=""/>
</paths>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2018-06-15
    • 2019-11-06
    • 2017-07-04
    • 1970-01-01
    相关资源
    最近更新 更多