网络地址图片二次采样(HttpURLConnection)
2017-10-24 20:16
281 查看
/* img即你要获得图片的控件 path即你要从网络上获取的图片 destHight你要采样的图片高度 deWeight你要采样的图片宽度 */ private void loadImage( ImageView img, String path, final int destHight, final int destWeight) { new AsyncTask<String, Void, Bitmap>() { @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (bitmap != null) img.setImageBitmap(bitmap); else img.setImageResource(R.mipmap.ic_launcher); } @Override protected Bitmap doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if (code == 200) { //得到图片数据 InputStream is = connection.getInputStream(); //BitmapFactory.decodeStream(is);//ARGB_888 一个像素占 4个字节 ARGB_565 一个像素占 2 个字节 Alpha Red、Green、Blue(颜色) BitmapFactory.Options options = new BitmapFactory.Options(); //在图片真正解析之前,不要解析图片数据,只需要告诉一下转换器,只要获取图片的像素(宽高) options.inJustDecodeBounds = true;//告诉BitmapFactory转换工厂不去真正解析图片只需要拿到图片宽高就行 //假解析 API 有bug BitmapFactory.decodeStream(is, null, options); //压缩,根据图片采样率进行二次采样 100 * 100 是你所期望的具体图片尺寸 //options.inSampleSize = 2; //采样用来计算图片的宽高(1920 * 1080 -> 1920 / 2 1080 / 2) //获取图片宽高,注意:图片的原始宽高 int width = options.outWidth; int height = options.outHeight; System.out.println("宽:" + width + "高 : " + height); //定义一个变量去记住我们采样率 int inSampleSize = 1;//默认是1 ,即不对图片进行任何压缩 if (width > destWeight || height > destHight) { int halfWidth = width / 2; int halfHeight = height / 2; while ((halfWidth / inSampleSize) >= destWeight && (halfHeight / inSampleSize) >= destHight) { //计算采样率 inSampleSize *= 2; } } 92a8 //采样率改变了 options.inSampleSize = inSampleSize;// 8 //图片压缩完之后,放行图片解析 options.inJustDecodeBounds = false;//告诉图片转换工厂,可以解析图片了 //关闭之前的流 is.close(); //重新再去得到当前这张图片的字节流数据 is = url.openStream(); //开始解析图片 Bitmap bitmap = BitmapFactory.decodeStream(is, null, options); //关闭流 is.close(); // Bitmap bitmap = BitmapFactory.decodeStream(is); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); }
相关文章推荐
- android网络HttpURLConnection抓取网络图片
- android HttpURLConnection编程详解,获取网络图片,html代码查
- HttpURLConnection加载网络图片
- 使用HttpURLConnection和AsyncTask从网络缓存图片
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- Handler+HttpURLConnection请求网络图片,字符串
- Android使用HttpURLConnection显示网络图片
- 通过HttpURLConnection获取网络图片实例
- 0314-使用HttpURLConnection下载网络图片并显示
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- AsyncTask与HttpURLConnection下载网络图片
- 使用HttpURLConnection加载网络图片
- Android HttpURLConnection获取网络下载图片POST请求方式
- HttpURLConnection获取网络读片,读取本地缓存图片
- 4.4网络图片查看器HttpURLConnection
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)