Java android应用程序下载apk文件并打开它(安装)

Java android应用程序下载apk文件并打开它(安装),java,android,Java,Android,我想知道你是否能帮忙,我正在尝试下载一个apk文件作为版本检查后的更新。 它正在下载apk,但它不会打开apk文件,因为在尝试打开apk时打印错误 错误: E/UpdateAPP: Update error! file:///storage/emulated/0/download/update.apk exposed beyond app through Intent.getData() 代码: public class UpdateApp extends AsyncTask<Strin

我想知道你是否能帮忙,我正在尝试下载一个apk文件作为版本检查后的更新。 它正在下载apk,但它不会打开apk文件,因为在尝试打开apk时打印错误

错误:

E/UpdateAPP: Update error! file:///storage/emulated/0/download/update.apk exposed beyond app through Intent.getData()
代码:

public class UpdateApp extends AsyncTask<String,Void,Void> {
    private Context context;
    public void setContext(Context contextf){
        context = contextf;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = Environment.getExternalStorageDirectory() + "/download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "update.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "/update.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);


        } catch (Exception e) {
            Log.e("UpdateAPP", "Update error! " + e.getMessage());
        }
        return null;
    }}

感谢您的帮助

因为API 26,“请求安装程序包”权限是实现安装apk文件所必需的权限

  • 在res/xml文件夹中创建“file_path.xml”以使用FileProvider api
  • 
    
  • 在AndroidManifest.xml中声明文件提供程序和权限
  • 注意。我使用了Androidx版本的FileProvider,如果您不使用Androidx,请确保
    android.support.v4.content.FileProvider

    
    
  • 通过FileProvider api获取“Uri”并请求安装权限

  • 编辑->删除在我的项目中使用的特定方法。

    由于API 26,“请求安装包”权限是实现安装apk文件所必需的权限

  • 在res/xml文件夹中创建“file_path.xml”以使用FileProvider api
  • 
    
  • 在AndroidManifest.xml中声明文件提供程序和权限
  • 注意。我使用了Androidx版本的FileProvider,如果您不使用Androidx,请确保
    android.support.v4.content.FileProvider

    
    
  • 通过FileProvider api获取“Uri”并请求安装权限

  • 编辑->删除在我的项目中使用的特定方法。

    可能重复的您解决了这个问题吗?可能重复的您解决了这个问题吗?其给出错误“解析包时出现问题”先生,您刚刚节省了时间!我能给你买杯啤酒吗?它的错误是“解析包裹时出现问题”先生,你刚刚救了一天!我能给你买杯啤酒吗?
    targetSdkVersion 27
    
    private fun openFile(file: File) {
            val uri = if (Build.VERSION.SDK_INT >= 24) {
                val authority = packageName + ".fileprovider"
                FileProvider.getUriForFile(this, authority, file)
            } else {
                Uri.fromFile(file)
            }
    
            val myMime = MimeTypeMap.getSingleton()
            val mimeType = myMime.getMimeTypeFromExtension(file.extension)
    
            val intent = Intent(Intent.ACTION_VIEW).apply {
                setDataAndType(uri, mimeType)
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }
    
            if (Build.VERSION.SDK_INT >= 26 && !packageManager.canRequestPackageInstalls()) {
                startActivity(
                    Intent(
                        Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
                        Uri.parse("package:$packageName")
                    )
                )
            } else {
                intent.action = Intent.ACTION_VIEW
                startActivity(intent)
            }
        }