下载新版本的应用程序并安装

时间:2018-08-10 07:49:11

标签: android filepath android-install-apk android-fileprovider

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

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

0 个答案:

没有答案
相关问题