Android - 自动更新问题
我使用此代码从 URL 保存 app.apk:
Context ctx;
InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream output = ctx.getApplicationContext().openFileOutput("myApp.apk", Context.MODE_WORLD_READABLE);
然后将其从输入保存到文件:
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
下载后,现在已成功完成,我想提示安装 - 我想我应该使用 FileInputStream,但我不知道究竟如何。
下载后,我有代码:
Intent install=new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(xxx - AND HERE I AM STUCK), "application/vnd.android.package-archive");
ctx.startActivity(install);
我尝试过 ctx.getApplicationContext().openFileInput("myApp.apk"),但 Uri.fromFile 需要文件,我给它 openFileInput。你知道如何解决吗?
谢谢
编辑:有人知道一些解决方案吗?
I use this code for saving app.apk from URL:
Context ctx;
InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream output = ctx.getApplicationContext().openFileOutput("myApp.apk", Context.MODE_WORLD_READABLE);
than I save it from input to file by:
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
And after downloading, which is successfuly finished now I want to prompt installation - I think I should use FileInputStream, but I don't know exactly how.
After downloading, I have code:
Intent install=new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(xxx - AND HERE I AM STUCK), "application/vnd.android.package-archive");
ctx.startActivity(install);
I have tried ctx.getApplicationContext().openFileInput("myApp.apk")
, but Uri.fromFile needs file and I am giving it openFileInput. Do you know how to solve it?
Thx
Edit: anyobody knows some some solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自: Android 获取应用程序的“主”数据目录
使用 getFilesDir() 返回openFileInput 保存文件的绝对路径。然后将其传递给您的 File() 构造函数。
From: Android Get Application's 'Home' Data Directory
Use getFilesDir() to return the absolute path to where openFileInput saved the file. Then pass this to your File() constructor.
我用这个:
它有效。
I use this:
and it works.