繁体   English   中英

在ImageLoader中加载URL后,如何获取原始位图大小? Android的

[英]How to get Original Bitmap size after loading URL in ImageLoader | Android

问候!!! 希望你们做的很棒!!!

我正在使用Universal Image Loader ,我需要从URL获取原始位图。

这是我的代码-

    imageLoaderNew.loadImage(bean.getPostMedia().get(i).getUrl(), optionsPostImg,
       new SimpleImageLoadingListener() {
       @Override
        public void onLoadingComplete(String imageUri,
                                      View view,
                                     Bitmap loadedImage) {
     // Do whatever you want with Bitmap
                                                      }
                                      });

的高度和宽度loadedImage 不一样的原始位图的高度和宽度。

我的原始图像高度宽度是2208,1108,但是图像加载器没有提供原始位图。

这是Image Loader的配置-

  optionsPostImg = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.post_img_default) //
                    .showImageForEmptyUri(R.drawable.post_img_default)
                    .showImageOnFail(R.drawable.post_img_default)

                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .imageScaleType(ImageScaleType.EXACTLY)

                    .build();

请让我知道如何获取原始位图。

如果原始图片太大,则不应下载,这会导致内存不足问题,但是无论如何,您可以从给定的URL下载原始文件。

public Bitmap getBitmapFromURL(String src) {
    try {
        java.net.URL url = new java.net.URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

如果遇到内存问题,可以使用以下命令

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

    return resizedBitmap;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM