You are trying to getInputStream()
from a HTTP connection while decoding its using BitmatpFactory
, so that BitmatpFactory
factory always had to wait for input stream to collect data.
And I don't see any close()
of input stream - expecting tin finally
block, that may cause further errors.
Try this:
Create HTTP connections in separated threads, so you can simultaneously download the images.
Decode bitmap only after file is downloaded (you may have to open another stream for Bitmap decoder but it's even faster and clearer then your current solution).
Lets also check your connection bandwidth to ensure what you are doing is limited by this factor (the network bandwidth).
[Update] These are some util functions:
/**
* Util to download data from an Url and save into a file
* @param url
* @param outFilePath
*/
public static void HttpDownloadFromUrl(final String url, final String outFilePath)
{
try
{
HttpURLConnection connection = (HttpURLConnection) (new URL(url)).openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
FileOutputStream outFile = new FileOutputStream(outFilePath, false);
InputStream in = connection.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) > 0)
{
outFile.write(buffer, 0, len);
}
outFile.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Spawn a thread to download from an url and save into a file
* @param url
* @param outFilePath
* @return
* The created thread, which is already started. may use to control the downloading thread.
*/
public static Thread HttpDownloadThreadStart(final String url, final String outFilePath)
{
Thread clientThread = new Thread(new Runnable()
{
@Override
public void run()
{
HttpDownloadFromUrl(url, outFilePath);
}
});
clientThread.start();
return clientThread;
}