`
弄月吟风
  • 浏览: 196918 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android文件下载

阅读更多

嘿嘿,也不知道什么时候写的代码,清理硬盘的时候发现了,而且还能用,就扔上来给大家看看吧、、

private void downFile() throws IOException{
		URL url = new URL(params); //设置Uri
		HttpURLConnection conn = (HttpURLConnection) url
				.openConnection();
		conn.setDoInput(true); 
		conn.connect();//连接
		InputStream inputStream = conn.getInputStream();//得到文件内容
		// bitmap = BitmapFactory.decodeStream(inputStream);

		// inputStream在一次使用以后就会变空
		byte[] data = readInstream(inputStream);
		File file = new File("/mnt/sdcard/a.jpg");
		FileOutputStream out = new FileOutputStream(file);
		out.write(data);
		out.close();
		// bitmap=BitmapFactory.decodeFile("/mnt/sdcard/a.jpg");
		// //获得文件夹下的照片
	}

	public byte[] readInstream(InputStream inputStream) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int length = -1;
		while ((length = inputStream.read(buffer)) != -1) {
			byteArrayOutputStream.write(buffer, 0, length);
			Log.i("OK", "OK");
		}
		byteArrayOutputStream.close();
		inputStream.close();
		Log.i("test", byteArrayOutputStream.toByteArray().toString());
		return byteArrayOutputStream.toByteArray();
	}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics