在我的应用程序中,我有一个图像 url 的 Arraylist,必须将其下载为位图并保存在另一个 Arraylist 中,以便我可以将它们设置为 mapview 中的地图标记。我的代码是
for( int i=0 ; i < Get_Feed_image.size() ; i++)
{
Get_Feed_image_bitmap.add(DownloadImage(Get_Feed_image.get(i)));
}
还有我的 DownloadImage 方法:
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;
}
我得到 Logcat 作为,
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
我得到我的力量关闭in.close();
如果有最少的图像,我的意思是数组中的 url 最少,它工作正常。或者,如果我的网络非常快,它可以正常工作。否则我会强制关闭。
建议我解决这个问题,在此先感谢。. . .