Android - Efficient way to load multiple images from remote server

前端 未结 2 556
借酒劲吻你
借酒劲吻你 2021-01-03 13:16

I have an Android application that would retrieve data (images+text) from a php remote server and display them in a GridView. I am doing the operation in the background usin

相关标签:
2条回答
  • 2021-01-03 13:44

    Best approach in my eyes would be to download the image files as normal image files via a HTTP get request. Make sure it is threaded of course, and have a thread pool that you can queue up requests into, and have 2-3 threads go through and download.

    In terms of saving them, I would personally move away from saving to blob in a database, and opt to save them to the persisted storage in your application's private directory. Saving the image files with their filename as their id in the database you have created will be much quicker for loading them back in.

    You can also hold a reference to the ImageView, and have it display a place-holder initially, with a successful HTTP request replacing the bitmap of the ImageView with the one you have just downloaded/read in from storage.

    You can also do some image caching within the HTTP request you make.

    ImageView myImageView = findViewById(R.id.testImage);
    URL url = new URL("http://www.website.com/image.jpg");
    URLConnection connection = url.openConnection();
    connection.setUseCaches(true);
    Object response = connection.getContent();
    if (response instanceof Bitmap) {
      Bitmap bitmap = (Bitmap)response;
      myImageView.setBitmap(bitmap);
    } 
    

    It also may be helpful to lookup the uses of the LRUCache, which performs a lot of caching functionality for you.

    Check out this link at the Android Developer site for a good in depth guide to image caching

    Edit: You can use the advice in Robert Rowntree's answer to load bitmaps more efficiently to cut down on your memory use as well. The link provided details loading of bitmaps using less memory, something that would work well if you are creating thumbnails from larger images downloaded over the web and saved off to local storage.

    0 讨论(0)
  • 2021-01-03 13:47

    IMO - there are 2 issues , moving the images across the network to the client and getting them loaded.

    Assuming that you are using http as the protocol, you should have a multithreaded solution for http as is available in apache httpclient package. That will get the pictures to the phone fast.

    Then , you have to present the pics by getting them into memory and a cache. Here you can consider what 'gallery3D' app does with its grid and bitmaps but its pretty complicated to read thru that code.

    check out - http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    check out code samples for loading thumbs from bitmaps.

    0 讨论(0)
提交回复
热议问题