Android下载网络图片到本地

Android下载网络图片到本地sdcard中

1.方法调用:

imageDownload(https://www.baidu.com/img/bdlogo.png);

 2.图片下载:

private void imageDownload(final String imgUrl){
		final String tempFileName = System.currentTimeMillis() + ".png";
		new Thread(new Runnable() {
			URL url;
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					url = new URL(imgUrl);
					InputStream is = url.openStream();
					Bitmap bitmap = BitmapFactory.decodeStream(is);
					saveFile(bitmap, tempFileName);
					handler.sendEmptyMessage(GETPIC_OK);
					is.close();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

 3.下载完成,handle处理:

Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (msg.what == GETPIC_OK) {
				if(!Utils.isEmpty(userImagePath)){
									}
			}

		};
	};

猜你喜欢

转载自chenzheng8975.iteye.com/blog/2235103