0

I've tried lots of different ways to download images and none of them actually worked, i have managed to get something working but its not perfect. I made a new thread for downloading the bitmaps, im not storing them on the system storage or caching them in the memory for later use. If i keep opening this activity over and over, the ram usage for this app keeps getting higher and higher, and i do not want that at all!

                URL url = new URL("http://10.0.0.21:80/1.png");
                bitmapOne = BitmapFactory.decodeStream(url.openStream());

                URL url2 = new URL("http://10.0.0.21:80/2.png");
                bitmapTwo = BitmapFactory.decodeStream(url2.openStream());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setImageBitmap(bitmapOne);
                        image2.setImageBitmap(bitmapTwo);
                    }});

Thank you all for your answers! but... I would only like to use pure java and no third party libraries for loading images, I was looking for a way to reduce memory usage and not so much of finding another way of downloading them. Im also not sure that having two urls and loading each stream looks very professional as the client could disconnect at any given moment (lets just pretend it does) and it does not load them both which are extremely required!

  • What you doing is pretty much the only way to download images using pure Android. Unless you want to start using other third party libs. but that depends on what you doing. If it just you making it and producing it then It should be ok with third party. If not check with your boss before using them. Best of Luck – MNM Jul 19 '16 at 02:10
  • 1
    Possible duplicate of [How to download and save an image in Android](http://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android) – mbmc Jul 19 '16 at 02:17
  • 1
    @MNM i edited the post, check back, but thanks for your comment! really helped me out – Alexis Heaton Jul 19 '16 at 02:56
  • The best advice I can give is put all your http calls in one place and only do it once. That is the most efficient use of a http call. just store all the values in a local storage and use them when needed. That will cut down on the constant calls to a from the server and your app – MNM Jul 19 '16 at 03:45
  • @MNM Thank you so much, you're really good at this stuff ;) – Alexis Heaton Jul 19 '16 at 04:24
  • Do you have different types of http calls? https and http or are they all going to be the same. If they are different then you may have to do different calls for the https based on the credentials of the url your calling. Https can be tricky in android. So unless your passing secure data I would just stick to http rather than https – MNM Jul 19 '16 at 05:24
  • You could save the image in DB as a "blob" and check whether you already have the image before calling the URL. RAM usage must be less than that when downloading image from a url. OR you could save the image as a base64 string somewhere and show the image from that.... Still less RAM will be used – Akhil Soman Jul 19 '16 at 05:41
  • He has no images. He has two bitmaps. Better compress them to jpg files or png files again. And load them the next time. Using a DB for this is terrible overhead. – greenapps Jul 19 '16 at 06:17

3 Answers3

1

Use Picasso library, everything will be a breeze:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

Picasso.with(this)
       .load("http://SomeUrl/CodeFeature.jpg")
       .into(imageView);

A good tutorial can be found here.

David
  • 15,894
  • 22
  • 55
  • 66
0

https://github.com/zetbaitsu/Compressor

Usage: compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);

This library maybe helpful for you.

You can visit GitHub. search "Android", sort by: "Most Stars" , lots of awesome library you will found.

Jay Ou
  • 63
  • 2
  • 9
0

Volley can be a good alternative too, with the ability of displaying animated gifs. This snippet illustrate the basic use, here with image cache, something that can make your app not to be memory and network too demanding:

    ImageLoader.ImageCache imageCache = new BitmapLruCache();
    ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
    NetworkImageView myImage = (NetworkImageView) findViewById(R.id.myImageView);
    myImage.setImageUrl(""+mSpeaker.getHeadshotUrl(), imageLoader);
    loadBitmap("http://yourdomain.com/yourImage.jpg", myImage);

For this to work all you need is the Volley library and BitmapLruCache.java

statosdotcom
  • 3,109
  • 2
  • 17
  • 40