android - 下载新版本应用并安装
问题描述
我构建了一个应用程序,并在运行应用程序时向用户显示对话框以下载新版本。好的,但我有一些问题。我想在“下载”文件夹的内部存储中下载一个应用程序,下载完成后自动安装它。但我无法访问它。我的代码:下载代码->
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
解决方案
推荐阅读
- javascript - 我无法在角度前端迭代数组
- r - 对R中一维列表中的列值求和
- anaconda - 如何在 Anaconda Jupyter 笔记本上运行金字塔自动 arima?
- reactjs - 从mapDispatchToProps更改为mergeProps时dispatch不是函数错误?
- python - 如何在 Keras 模型中使用 tf.keras.layers.DepthwiseConv2D?得到 AttributeError:“节点”对象没有属性“输出掩码”
- c++ - 在 C++ 中使用 SDL2 和 OpenGL 运行循环时调整窗口大小?
- python - 使用时间增量对时间范围进行分组?
- bootstrap-4 - 允许 Bootstrap4 的弹出框溢出屏幕
- python - 如果表为空,则执行 INSERT 多个值 POSTGRES
- sql-server - 某些 SQL Server 2016 列无法附加到 Access 数据库