1

I started to build a game, this game gets images from server.

I used Bitmap to convert the IMAGE*S* and its works slowly.

Its take 25 - 40 seconds to load 22 images (100KB for each image).


public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Implementation:


Bitmap pictureBitmap = ImageFromUrl.getBitmapFromURL(path);

PS..

I used LazyList before ,and it's not for my goal.

More offers?

Tnx....

1 Answers1

1

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;
}
Kelvin Trinh
  • 1,248
  • 7
  • 15