Android报错: exposed beyond app through Intent.getData()

AndroidManifest.xml

在<application 里添加

android:networkSecurityConfig="@xml/fileproviderpath"
	<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.silentinstallation.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/fileproviderpath"
                />
        </provider>

res/xml/fileproviderpath

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="root" path="."></root-path>
    <files-path
        name="file"
        path="."></files-path>
    <files-path
        name="files"
        path="."></files-path>
    <cache-path
        name="cache"
        path="."></cache-path>
    <external-path
        name="external"
        path="."></external-path>
    <external-cache-path
        name="external_cache"
        path="."></external-cache-path>
    <external-files-path
        name="external_file"
        path="."></external-files-path>
    <external-path name="external_files" path="."/>
</paths>

安装APK代码

String path = "/sdcard/youku.apk";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("ISSILENT",1);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri uri;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            uri = FileProvider.getUriForFile(this,"com.example.silentinstallation.fileprovider",new File(path));
            intent.setDataAndType(uri,"application/vnd.android.package-archive");
        }else{
            uri = Uri.parse("file://" + path);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个错误是由于在 Android 7.0(API级别24)及更高版本中,Google 引入了一项新的安全特性,称为“File Provider”。它要求您使用 `FileProvider` 来共享文件,以提高应用程序的安全性。 要解决这个问题,您需要使用 `FileProvider` 来生成适当的 `Uri`,并将其传递给 `Intent`。以下是修改后的代码: ```java public void openFile(String filePath) { File file = new File(filePath); Uri uri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, getMimeType(filePath)); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP); try { startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(this, "No application found to open this file.", Toast.LENGTH_LONG).show(); } } ``` 在上面的代码中,您需要将 `com.example.myapp.fileprovider` 替换为您在 AndroidManifest.xml 文件中定义的 FileProvider 的 authorities。 同时,您需要在 AndroidManifest.xml 文件中添加以下内容来定义 FileProvider: ```xml <application> ... <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.myapp.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application> ``` 请确保在 res/xml 文件夹下创建一个名为 `file_paths.xml` 的 XML 文件,并在其中定义您要共享的文件路径。例如: ```xml <paths> <external-path name="external_files" path="." /> </paths> ``` 这样修改后,您应该能够正确地使用 File Provider 来共享文件,并解决 "exposed beyond app through Intent.getData()" 错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值