欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android下载网络图片到本地

程序员文章站 2022-06-04 15:31:07
...

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)){
									}
			}

		};
	};