android开发下载文件损坏解决方法,Android中已下载的.apk文件已损坏
我需要通過程序下載一個(gè).apk文件然后啟動(dòng)它的活動(dòng)。
我使用的代碼如下
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;
}我提到我先為文本文件調(diào)用方法,然后再調(diào)用apk文件。因此,每次我在本地處理文件時(shí),我都知道發(fā)生了什么問題。通過這種方式,我知道文本文件已正確下載。但.apk文件已損壞。因?yàn)槲以诒镜亻_發(fā),可以訪問DDMS和本地主機(jī)(IP:10.0.2.2),所以我可以堅(jiān)信罪魁禍?zhǔn)拙褪巧厦娴拇a。當(dāng)我通過DDMS與原始.apk文件人為地替換“下載”文件時(shí),所有后續(xù)處理都是OK。另外,當(dāng)我比較原始文件和下載的.apk文件時(shí),我遇到了不同之處。
我究竟做錯(cuò)了什么?
謝謝
PS:搜索,我意識到,雖然是一個(gè)流行的問題,但沒有一致的答案。就我而言,我將其確定為純粹的下載方法問題。
總結(jié)
以上是生活随笔為你收集整理的android开发下载文件损坏解决方法,Android中已下载的.apk文件已损坏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.lang.NoSuchMeth
- 下一篇: 初识-Android之智能短信项目相关技