我從我的android應用下載zip文件。點擊下載按鈕後,活動停止。以下是下載zip文件的代碼。嘗試從url下載zip文件時停止活動
下載代碼:
public void myDownload(View v) {
URL url;
try {
url = new URL("https://github.com/jquery/jquery/archive/master.zip");
try {
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File("Master.zip"));
System.out.println("Reading the file.");
int length = -1;
byte[] buffer = new byte[1024];// buffer for portion of data from
// connection
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
System.out.println("File Downloaded");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
感謝您的幫助!
請分享日誌 – user1767260
我認爲它的NetworkOnMainThread,更好的是你把這段代碼放在一個單獨的AsyncTask –