android请求获取图片的方法

public static Bitmap getUrlBitmap(String path) {
    Bitmap bm = null;
    try {
        URL url = new URL(path);//创建一个URL对象,其参数为网络图片的链接地址
        //使用一个URL对象开启一个链接
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //设置相关参数
        con.setDoInput(true);
        con.setUseCaches(true);  //不允许使用缓存
        con.setConnectTimeout(5000);//设置超时
        con.setReadTimeout(10000);
        con.connect();
        int code = con.getResponseCode();
        if (code==200) {
            InputStream is = con.getInputStream();//获取输入流
            bm = getFitSampleBitmap(is);//将输入流解码为Bitmap对象
            is.close();
        }
        con.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bm;
}

/***
 * 读取获得Bitmap对象
 * @param inputStream
 * @return
 * @throws Exception
 */
public  static  Bitmap  getFitSampleBitmap(InputStream  inputStream) throws Exception{
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//设置该项可以获得图片原始宽高度
    byte[] bytes = readStream(inputStream);
    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    options.inSampleSize=computeSampleSize(options, -1, 128*128);
    
    options.inJustDecodeBounds=false;
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}

/**
 * 从inputStream中获取字节流 数组大小
 **/
public static byte[] readStream(InputStream inStream) throws Exception{
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

/***
 * 计算图片大小
 * @param options
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
    int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
    int roundedSize;
    if (initialSize <= 8) {
        roundedSize = 1;
        while (roundedSize < initialSize) {
            roundedSize <<= 1;
        }
    } else {
        roundedSize = (initialSize + 7) / 8 * 8;
    }
    return roundedSize;
}

/***
 * 计算获得图片大小
 * @param options
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;
    int lowerBound = (maxNumOfPixels == -1) ?
            1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));

    int upperBound = (minSideLength == -1) ?
            128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }
    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android获取网络图片方法有很多,这里介绍其中两种比较常用的方法。 第一种方法是使用 Android 提供的网络请求库 Volley,Volley 可以方便地进行网络请求图片加载。具体步骤如下: 1. 添加 Volley 库的依赖,在 app 的 build.gradle 文件中添加以下代码: ```groovy dependencies { implementation 'com.android.volley:volley:1.2.0' } ``` 2. 在代码中使用 Volley 加载图片,示例代码如下: ```java String imageUrl = "http://example.com/image.jpg"; ImageView imageView = findViewById(R.id.image_view); ImageRequest imageRequest = new ImageRequest( imageUrl, response -> imageView.setImageBitmap(response), 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.RGB_565, error -> Log.e(TAG, "Image load error") ); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(imageRequest); ``` 这段代码中,我们通过 ImageRequest 构造器创建了一个加载图片请求,然后将其添加到 Volley 的请求队列中。当请求完成后,我们将得到一个 Bitmap 对象,可以将其设置给 ImageView。 第二种方法是使用第三方图片加载库 Glide,Glide 功能强大,支持加载图片、GIF、视频等多种类型的资源。具体步骤如下: 1. 添加 Glide 库的依赖,在 app 的 build.gradle 文件中添加以下代码: ```groovy dependencies { implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' } ``` 2. 在代码中使用 Glide 加载图片,示例代码如下: ```java String imageUrl = "http://example.com/image.jpg"; ImageView imageView = findViewById(R.id.image_view); Glide.with(this).load(imageUrl).into(imageView); ``` 这段代码中,我们通过 Glide.with(this) 方法创建了一个 Glide 请求管理器,然后使用 load() 方法加载图片,最后使用 into() 方法图片设置给 ImageView。Glide 会自动处理图片的缓存和压缩等问题,使用起来非常方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值