获取网络图片(Bit地图)至内存或者SD卡

获取网络图片(Bitmap)至内存或者SD卡
	/**
	 * 获取网络图片
	 * 注意权限:
	 * <uses-permission android:name="android.permission.INTERNET"/>
	 */
	private Bitmap getBitmapFromNetWork(String imageUrl){
		URL url=null;
		Bitmap bitmap=null;
		InputStream inputStream=null;
		HttpURLConnection httpURLConnection=null;
		ByteArrayOutputStream byteArrayOutputStream=null;
		try {
			url=new URL(imageUrl);
			httpURLConnection=(HttpURLConnection) url.openConnection();
			httpURLConnection.setConnectTimeout(5*1000);
			httpURLConnection.setReadTimeout(10*1000);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			if (httpURLConnection.getResponseCode()==HttpStatus.SC_OK) {
				inputStream=httpURLConnection.getInputStream();
				byteArrayOutputStream=new ByteArrayOutputStream();
				int len=0;
				byte [] buffer=new byte[1024];
				while((len=inputStream.read(buffer))!=-1){
					byteArrayOutputStream.write(buffer, 0, len);
					byteArrayOutputStream.flush();
				}
				byte [] imageData=byteArrayOutputStream.toByteArray();
				bitmap=BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
			} else {
				System.out.println("图片请求失败");
			}
		} catch (Exception e) {
			    System.out.println("e="+e.toString());
		}finally{
			try {
				if (byteArrayOutputStream!=null) {
					byteArrayOutputStream.close();
				}
				if (inputStream!=null) {
					inputStream.close();
				}
				if (httpURLConnection!=null) {
					httpURLConnection.disconnect();
				}
			} catch (Exception e) {
				 System.out.println("e="+e.toString());
			}
		}
		
		return bitmap;
	}
	
	
	/**
	 * 获取网络图片且保存至SDCard
	 * 注意权限:
	 * <uses-permission android:name="android.permission.INTERNET"/>
	 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
	 */
	private void getBitmapFromNetWorkAndSaveToSDCard(String imageUrl,String filePath){
		URL url=null;
		File imageFile=null;
		HttpURLConnection httpURLConnection=null;
		FileOutputStream fileOutputStream=null;
		BufferedOutputStream bufferedOutputStream=null;
		InputStream inputStream=null;
		BufferedInputStream bufferedInputStream=null;
		try {
			url=new URL(imageUrl);
			httpURLConnection=(HttpURLConnection) url.openConnection();
			httpURLConnection.setConnectTimeout(5*1000);
			httpURLConnection.setReadTimeout(10*1000);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			if (httpURLConnection.getResponseCode()==HttpStatus.SC_OK) {
				imageFile=new File(filePath);
				if (!imageFile.getParentFile().exists()) {
					imageFile.getParentFile().mkdirs();
				}
				if (!imageFile.exists()) {
					imageFile.createNewFile();
				}
				fileOutputStream=new FileOutputStream(imageFile);
				bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
				inputStream=httpURLConnection.getInputStream();
				bufferedInputStream=new BufferedInputStream(inputStream);
				int len=0;
				byte [] buffer=new byte[1024];
				while((len=bufferedInputStream.read(buffer))!=-1){
					bufferedOutputStream.write(buffer, 0, len);
					bufferedOutputStream.flush();
				}
			} else {
				System.out.println("图片请求失败");
			}
		} catch (Exception e) {
			    System.out.println("e="+e.toString());
		}finally{
			try {
				if (fileOutputStream!=null) {
					fileOutputStream.close();
				}
				if (bufferedOutputStream!=null) {
					bufferedOutputStream.close();
				}
				if (inputStream!=null) {
					inputStream.close();
				}
				if (bufferedInputStream!=null) {
					bufferedInputStream.close();
				}
				if (httpURLConnection!=null) {
					httpURLConnection.disconnect();
				}
			} catch (Exception e) {
				 System.out.println("e="+e.toString());
			}
		}
		
	}