android开发下载文件损坏解决方法,Android中已下载的.apk文件已损坏
我需要通過程序下載一個.apk文件然后啟動它的活動。
我使用的代碼如下
private String downloadFile(String sourceURL, String destinationPath)
{
try {
URL url = new URL(sourceURL);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destinationPath);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) > 0) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();// EDITED here:制作文件rw,否則apk文件將不會被安裝
Runtime.getRuntime()。exec(“chmod 666”+ destinationPath);
message = "OK";
}
catch (MalformedURLException e) {
message = "Malformed URL error: " + e.getMessage();
}
catch (ProtocolException e) {
message = "Protocol exception error: " + e.getMessage();
}
catch (FileNotFoundException e) {
message = "File not found error: " + e.getMessage();
}
catch (IOException e) {
e.printStackTrace();
message = "Failed to download the file : " + e.getMessage();
}
return message;
}我提到我先為文本文件調用方法,然后再調用apk文件。因此,每次我在本地處理文件時,我都知道發生了什么問題。通過這種方式,我知道文本文件已正確下載。但.apk文件已損壞。因為我在本地開發,可以訪問DDMS和本地主機(IP:10.0.2.2),所以我可以堅信罪魁禍首就是上面的代碼。當我通過DDMS與原始.apk文件人為地替換“下載”文件時,所有后續處理都是OK。另外,當我比較原始文件和下載的.apk文件時,我遇到了不同之處。
我究竟做錯了什么?
謝謝
PS:搜索,我意識到,雖然是一個流行的問題,但沒有一致的答案。就我而言,我將其確定為純粹的下載方法問題。
總結
以上是生活随笔為你收集整理的android开发下载文件损坏解决方法,Android中已下载的.apk文件已损坏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.lang.NoSuchMeth
- 下一篇: android sina oauth2.