Thread run=new Thread(){
@Override
public void run() {
InputStream is = null;
OutputStream fos = null;
File file = new File(savePath);
if(!file.exists()){
file.mkdirs();
}
try {
URL url = new URL(myurl);
URLConnection urlConn = url.openConnection();
is = urlConn.getInputStream();
int length = urlConn.getContentLength(); //文件大小
File oldFile = new File(savePath+fileName);
if(oldFile.exists())
{
oldFile.delete();
}
fos = openFileOutput(fileName,Context.MODE_WORLD_READABLE);
int count = 0,numread = 0;
byte buf[] = new byte[1024];
while((numread = is.read(buf))!=-1){
count+=numread;
int progressCount =(int)(((float)count / length) * 100);
changeProgress(progressCount);
fos.write(buf, 0, numread);
}
fos.flush();
Message msg1 = newhandler.obtainMessage(1);
newhandler.sendMessage(msg1);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fos!=null){
fos.close();
}
if(is!=null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
};