Reputation: 43
The images comes from different URLs and i want to display all of them in a ListView
. Every URL
represents one image, How can i display all of them using image URL where every image has a unique ID ?.
"http://www.samplesite.com/Customer/File/DownloadResource/"imageID"?token="+m_token;
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: java.io.FileNotFoundException: http://samplesite.com/Customer/File/DownloadResource/220?token=aGkvWUlQSW9EeVZmU3pQOFF6WWo5OU4raGdzVjBrSngzWEQvbkNueWxYdz06dGVzdDFAYXBpLmNvbTo2MzYwNjg0OTU0MDgyMDAwMDA6Qw==
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at com.app.tysomcustomer.dashboard.LoyaltyFragment$ConvertImageUrlTask.doInBackground(LoyaltyFragment.java:320)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at com.app.tysomcustomer.dashboard.LoyaltyFragment$ConvertImageUrlTask.doInBackground(LoyaltyFragment.java:277)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
08-15 17:19:01.301 25505-25959/com.app.projectactivity W/System.err: at java.lang.Thread.run(Thread.java:818)
Upvotes: 1
Views: 371
Reputation: 2121
To download multiple image file asynchronously, you should use libraries such as Universal Image Loader, Fresco, Picasso, Glide, Volley Image Loader etc.
My personal favorite is Universal Image Loader
It gives you options to perform certain task like onLoadingStarted, onLoadingFailed, onLoadingComplete, onLoadingCancelled
and many more.
This is the best way to load images from server without bothering about asycTask and it works well in list view.
Upvotes: 1
Reputation: 2532
Use HttpURLConnection
to obtain an input stream from the URL. Then use BitmapFactory
to decode this stream into a bitmap.
Keep in mind not to run this code in your main thread. Otherwise it will block anything until the image has been downloaded and decoded.
try {
URL url = new URL("http://www.samplesite.com/Customer/File/DownloadResource/" + imageID + "?token="+ m_token);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Bitmap mBitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 1