读书人

Android文件上载

发布时间: 2012-07-22 19:31:17 作者: rapoo

Android文件下载

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

private void downFile() throws IOException{URL url = new URL(params); //设置UriHttpURLConnection 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();}
?

读书人网 >Android

热点推荐