把代码过程经常用到的内容做个备份,下面的代码内容是关于Android多线程从网络下载图片到本地存储的代码。
private static final String TAG = "DownloadToPhone"; protected static final String IMAGE_FILENAME = "news-thumb.png";
private static final String PACKAGE_NAME = "com.myWebview";
public void loadfile(){
Runnable getImage = new Runnable(){
public void run() {
try {
URL u = new URL(IMAGE_SOURCE);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = openFileOutput(IMAGE_FILENAME, Activity.MODE_WORLD_WRITEABLE);
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
}catch (Exception e) {
Log.e(TAG, "could not download and save IMAGE file", e);
}
}
};
new Thread(getImage).start();
}
复制代码
}