获取网络图片,并保存到本地

2019-04-15 15:18发布

AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); asyncHttpClient.get("http://c.hiphotos.baidu.com/image/pic/item/242dd42a2834349b49f953d4cbea15ce37d3bee7.jpg", new BinaryHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) { try { Bitmap bitmap= BitmapFactory.decodeByteArray(binaryData,0,binaryData.length); FileOutputStream fileInputStream=new FileOutputStream(new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg")); BufferedOutputStream bufferedInputStream=new BufferedOutputStream(fileInputStream); bitmap.compress(Bitmap.CompressFormat.JPEG,100,bufferedInputStream); fileInputStream.flush(); fileInputStream.close(); bufferedInputStream.flush(); bufferedInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) { } });