0
public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection)
                    url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
            return imageBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

I use this function to get image from url as a bitmap. For a large image bitmap return null. How can ı solve this?

uyarc
  • 579
  • 4
  • 17
  • What do you mean by a large image? A large image is about 4-5MB. – Khaby Lame Sep 27 '21 at 10:31
  • @KhabyLame image size 4961 × 3508 and about 1.5MB. – uyarc Sep 27 '21 at 10:41
  • 1.5MB isn't much – Khaby Lame Sep 27 '21 at 10:44
  • Bitmap.decodeStream() indeed returns null if the bitmap would become too big for available memory in the used device. Not file size is important but resolution. You have to tell what you wanna do with the file before we can advice you how to solve your problem. – blackapps Sep 27 '21 at 10:44
  • 1
    BitmapFactory can handle upto 100MB. I have tried it with my canon camera photo. Its about 150MB and BitmapFactory can convert it to bitmap. It depends on the user's connection – Khaby Lame Sep 27 '21 at 10:45
  • 4961 × 3508 x 4 = ??? – blackapps Sep 27 '21 at 10:45
  • You have to put your code in a try catch block. If there is a exception, then handle it – Khaby Lame Sep 27 '21 at 10:45
  • You can use Glide. Its efficient, fast and caches images. – Khaby Lame Sep 27 '21 at 11:00
  • Does this answer your question? [How to make Bitmap compress without change the bitmap size?](https://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size) – Khaby Lame Sep 27 '21 at 11:01
  • @KhabyLame It can be depend on the user's connection you are right but I want to prevent return null for bitmap. – uyarc Sep 27 '21 at 17:30

2 Answers2

0

Try this:

 private Bitmap getBitmap(String image_link)
    {
        URL website;

        try {
            website = new URL(image_link);
            HttpURLConnection connection = (HttpURLConnection) website.openConnection();
            InputStream is = connection.getInputStream();

            if(req_width == 0)
            {
                return BitmapFactory.decodeStream(is);
            }


            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);
            is.close();


            connection = (HttpURLConnection) website.openConnection();
            is = connection.getInputStream();
            options.inSampleSize = calculateInSampleSize(options, req_width, req_height);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(is, null , options);

        } catch (Exception  e) {
            return null;
        }
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Vijay S
  • 358
  • 3
  • 10
  • I use it but same device for different version of Android not load image only show black screen. Is there any way to calculate reqHeight and reqWidth automatically. – uyarc Sep 27 '21 at 17:32
0

Code modified. See if this works If the bitmap is null, this code will replace it with another sample bitmap.

  public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection)
                    url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
            if(bitmap == null){
                imageBitmap = 
  BitmapFactory.decodeResource(context.getResources(),R.drawable.your_sample_image);
    
            return imageBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
Khaby Lame
  • 228
  • 2
  • 16