我使用的功能将图像从服务器下载到我的Android设备的内部存储器,功能是:
private void downloadFile(String myurl, String name) {
// TODO Auto-generated method stub
int count=0;
try {
URL url = new URL(myurl);
URLConnection conexion = url.openConnection();
conexion.connect();
lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test library/"+name.replace(" ","")+".png");
byte data[] = new byte[1024];
total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {a=e.toString();}
}
我在服务器上有22个图像,问题是有些图像被下载为损坏的文件,请帮忙吗?