Android 安装APK会导致分析错误

Android 安装APK会导致分析错误,android,apk,Android,Apk,我在/sdcard/Download 我有以下代码: @Override public void onClick(View v){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/sdcard/Download/" + "ap

我在
/sdcard/Download

我有以下代码:

@Override
public void onClick(View v){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/sdcard/Download/" + "app-debug.apk")),
    "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
我得到这个错误:

解析错误

分析包时出现问题


如何以编程方式修改它而不出现此错误?

对于Android N及更高版本,您应该像这样使用
FileProvider

        Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
在您的AndroidManifest.xml中:

    <application ...>
    <provider
        android:name="androidx.core.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>
    </application>
不要忘记在清单中添加权限:

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


此消息表示apk已损坏。除非它是你的,否则你不能对它做太多,然后确保它没有损坏@k0sh apk已损坏?但我的apk很简单。仅显示更新文本可能未生成?为什么不重新尝试生成?您是否已声明在sd卡中写入的清单权限。是否已启用从未知源安装应用程序?代码很好,不应该与您的错误有任何关系。如果它有机会执行,就不会有解析错误。
setFlag()
会导致安全异常,而不是使用为我工作的
addFlag
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />