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 . . . .
Instead of preparing
ArrayList<Bitmap>, you should manage onlyArrayList<String>i.e. UrlsWhy 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.