1

I would like to download an image from an url, using this example:

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

Unfortunately the returned myBitmap is null, and I have no idea why. The url is a local ip: http://192.168.0.101:7777/my_image.png

  • I get no error messages
  • If I open this link in the browser, it displays it.
  • android:usesCleartextTraffic="true" is enabled, and my json requests work with Volley.
  • I tried to use BufferedInputStream, but that also does not work
Iter Ator
  • 8,226
  • 20
  • 73
  • 164

2 Answers2

0

If the bitmap would become to big for available memory BimapFactory.decodeStream() returns null.

So you are trying to load a picture with a big resolution.

Try to load a small picture and it will go.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I build with this parameter: `org.gradle.jvmargs=-Xmx2048m` The image is only 1.5 MB, nowhere near 2 GB – Iter Ator Sep 01 '20 at 07:42
  • File size does not matter. It's the resolution that counts like 4028x3246 and so. Bitmaps become easily 100 MB and more. Now did you try with a small image? A thumbnail for instance? – blackapps Sep 01 '20 at 07:46
  • @IterAtor: "I build with this parameter: org.gradle.jvmargs=-Xmx2048m" -- that controls the memory usage of your IDE. It has nothing to do with memory usage inside of your app running on Android. – CommonsWare Sep 01 '20 at 12:52
0

How simple and cool is this? when you are using Glide

      ImageView imageView = findViewById(R.id.imageView);
      Glide.with(this)
           .asBitmap()
           .load("https://www.google.com/images/srpr/logo11w.png")
           .into(new CustomTarget<Bitmap>() {
         @Override
         public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
            imageView.setImageBitmap(resource);
         }
         @Override
         public void onLoadCleared(@Nullable Drawable placeholder) {
         }
      });

Or you can try these option as well

public static Bitmap loadImage(String imageUrl) {
        Bitmap bitmap = null;
        try {
                URL url = new URL(imageUrl);
                bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (MalformedURLException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
        return bitmap;
    }

or

public static Bitmap downloadImage(String urlImage) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            URL url = new URL(urlImage);
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(new FlushedInputStream(stream), null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }
    
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81