0

In my app , i have an Arraylist of image urls and that has to be downloaded as bitmap and save in another Arraylist , so that i could set them as the map-markers in the mapview . My code is

for( int i=0 ; i < Get_Feed_image.size() ; i++)
{
    Get_Feed_image_bitmap.add(DownloadImage(Get_Feed_image.get(i)));
}

And my DownloadImage method :

private Bitmap DownloadImage(String URL) 
    {
        Bitmap bitmap = null;
        InputStream in = null;
        try 
        {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        }
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException 
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try 
        {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) 
            {
                in = httpConn.getInputStream();
            }
        }
        catch (Exception ex) 
        {
            throw new IOException("Error connecting");
        }
        return in;
    }

And i get Logcat as ,

12-31 17:29:43.269: E/AndroidRuntime(14572): FATAL EXCEPTION: AsyncTask #5
12-31 17:29:43.269: E/AndroidRuntime(14572): java.lang.RuntimeException: An error occured while executing doInBackground()
12-31 17:29:43.269: E/AndroidRuntime(14572):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.lang.Thread.run(Thread.java:1019)
12-31 17:29:43.269: E/AndroidRuntime(14572): Caused by: java.lang.NullPointerException
12-31 17:29:43.269: E/AndroidRuntime(14572):    at com.live.Feed.DownloadImage(Feed.java:279)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at com.live.Feed.access$1(Feed.java:271)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at com.live.Feed$Startsyntask.doInBackground(Feed.java:383)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at com.live.Feed$Startsyntask.doInBackground(Feed.java:1)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-31 17:29:43.269: E/AndroidRuntime(14572):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-31 17:29:43.269: E/AndroidRuntime(14572):    ... 4 more

i get my Force Close at in.close();

IF there are minimal Images , i mean minimal urls in the array , it works fine . Or if my Network is Very fast , it works fine .Otherwise i get Force close .

Suggest me to fix this issue , Thanks in Advance . . . .

VIGNESH
  • 2,023
  • 8
  • 31
  • 46
  • Have a look at [Sir Paresh Mayani's LazyLoading](http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/) and [fedor's lazy loading](http://stackoverflow.com/a/3068012/593709). – Adil Soomro Dec 31 '12 at 12:36
  • @AdilSoomro Call me Paresh :) And it was an example using the same lazy loading of Fedor. All the credits goes to Fedor only! Thanks – Paresh Mayani Feb 13 '14 at 05:05

3 Answers3

0

Instead of preparing ArrayList<Bitmap>, you should manage only ArrayList<String> i.e. Urls

Why you should manage ArrayList? and How?

You should use the image caching algorithm so that once image is downloaded it will get cached and load it from cache as and when required. It would be easy to manage actually.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

You got an error doing Asynctask, your code isn't doing the right job.

Try to use a library instead, try https://github.com/nostra13/Android-Universal-Image-Loader.

Or this one http://android-developers.blogspot.com.br/2010/07/multithreading-for-performance.html, it have a tutorial and a demo code, I´ve already tested and works perfect in all API's.

Marckaraujo
  • 7,422
  • 11
  • 59
  • 97
0

You should close your Inputstream in a finnaly block, to make sure it will close.

You also should return the InputStream of the OpenHTTPConnection in the ifclause and else throw an Exception, on this way you can be sure that the connection has been opened and is ok, as you do it could also return null which could cause your problem.

Try it and give some feedback.

Fussel
  • 123
  • 1
  • 1
  • 9