可能重复问题:
Android:以编程方式安装.apk文件
我需要更新我的Android应用程序。在程序内部,我下载了新版本。如何以编程方式将当前版本替换为已下载的新版本?
URL url = new URL("http://www.mySite.com/myFolder/myApp.apk");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
FileOutputStream fos = this.getApplicationContext().openFileOutput("myApp.apk", Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1)
{
// EDIT - only write the bytes that have been written to
// the buffer, not the whole buffer
fos.write(buffer, 0, len); // file to save app
}
fos.close();
.... here I have the file of new app, now I need use it
fos.write(buff, 0, len)
,其中len
在最后一块被写入时可能小于buffer.length
。 - David O'Meara