简体   繁体   中英

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

Greetings!!! Hope you guys are doing Great!!!

I am working with Universal Image Loader , i need to get the original Bitmap from the URL.

Here is my code -

    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
                                                      }
                                      });

The Height and Width of loadedImage is not same as the Height and Width of original Bitmap.

My Original Image Height Width is 2208,1108 but the Image Loader are not giving the Original Bitmap.

Here is the Configuration of 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();

Please let me know, how to get the original Bitmap.

You should not download the original image if it is too big it will cause out of memory issues but anyway this is you can do to download original file from the given 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;
    }
}

And if you get memory issues then you can use following

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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