首页 > 解决方案 > 下载新版本应用并安装

问题描述

我构建了一个应用程序,并在运行应用程序时向用户显示对话框以下载新版本。好的,但我有一些问题。我想在“下载”文件夹的内部存储中下载一个应用程序,下载完成后自动安装它。但我无法访问它。我的代码:下载代码->

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, "pezeshk-yab.apk");
            if (outputFile.exists()) {
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();
            int total_size = 8404140;//size of apk
            byte[] buffer = new byte[1024];
            int len1;
            int per;
            int downloaded = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                downloaded += len1;
                per = downloaded * 100 / total_size;
                publishProgress(per);
            }
            fos.close();
            is.close();
            newVersion(PATH);
            flag = true;

并且对于自动安装使用此代码,我知道这些代码有问题,所以请更正它们:

AndroidManifest.xml ->

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="ir.vian_web.pezeshkyab.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>

我对这部分的大部分问题是我不知道在这里放什么 filepaths.xml ->

 <?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="my_app"
        path="" />
</paths>

和新版本的方法,这部分我的大部分问题是我不知道放什么 ->

 public void newVersion(String path) {
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    File file = new File(path, "pezeshk-yab.apk");
    Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", file);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
    finish();
}

调试时我的错误->

Failed to find the configured root that contains /storage/emulated/0/Download/pezeshk-yab.apk

标签: androidfilepathandroid-install-apkandroid-fileprovider

解决方案


推荐阅读