Reputation: 5892
This is piece of code is using to download images , can somebody tell me how to optimize this code to decrease download time for each image.
URL url;
HttpURLConnection connection = null;
InputStream input = null;
System.setProperty("http.keepAlive", "true");
try {
url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(HTTP_CONNECTION_TIMEOUT);
connection.setRequestProperty("Connection", "Keep-Alive");
input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
currentRequestInProgress.remove(urlString);
if (connection != null)
connection.disconnect();
if(input != null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 381
Reputation: 470
I was also busy suggesting Picasso when OrhancC1 beat me to it, so if you end up using it credit him.
Picasso is great! We are using it in one of our projects and managed to get the HTTP caching working correctly. In our application the images we download don't change very frequently so once loaded the first time any subsequent loads are resolved from the cache which is a great deal faster than the network.
If for whatever reason that is not an option the answers to this question may be relevant: Speed Up download time.
Upvotes: 1
Reputation: 1410
Personally, I would just use something like Picasso. Give it a shot
http://square.github.io/picasso/
Upvotes: 0