当前位置: 代码迷 >> Android >> Android文件上载
  详细解决方案

Android文件上载

热度:70   发布时间:2016-05-01 20:04:56.0
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();	}
?
  相关解决方案