android中从网络服务器获取图像时出现异常
您好,我在从服务器获取图像时遇到以下异常。我在获取 5-7 张图像后得到以下结果:
06-12 16:35:50.904: 警告/异步任务(717): java.lang.InterruptedException 06-12 16:35:50.904:警告/AsyncTask(717):
在 java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptically(AbstractQueuedSynchronizer.java:1254) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:219) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask.get(FutureTask.java:82) 06-12 16:35:50.904: 警告/异步任务(717):位于 android.os.AsyncTask$3.done(AsyncTask.java:196) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask$Sync.innerCancel(FutureTask.java:293) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask.cancel(FutureTask.java:75) 06-12 16:35:50.904: 警告/异步任务(717):位于 android.os.AsyncTask.cancel(AsyncTask.java:325) 06-12 16:35:50.904: 警告/异步任务(717):位于 us.bnn.news.IncidentDetailsActivity$GetImages.doInBackground(IncidentDetailsActivity.java:1691) 06-12 16:35:50.904: 警告/异步任务(717):位于 us.bnn.news.IncidentDetailsActivity$GetImages.doInBackground(IncidentDetailsActivity.java:1) 06-12 16:35:50.904: 警告/异步任务(717):位于 android.os.AsyncTask$2.call(AsyncTask.java:185) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.FutureTask.run(FutureTask.java:137) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 06-12 16:35:50.904: 警告/异步任务(717):位于 java.lang.Thread.run(Thread.java:1096)
我使用这种方法:
public static Bitmap getLargeImage(String imgUrl)
{
Bitmap bitmap = null;
InputStream in = null;
Log.v(TAG,"start fetchimg main images");
try
{
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet(imgUrl);
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = mHttpResponse.getEntity();
if ( entity != null)
{
in = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(in, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
if(in != null)
in.close();
}
}
}
catch (Exception ex)
{
Log.v("Error Fetching images from url",ex.getMessage());
}
return bitmap;
}
Hi i am getting the following exception while fetching the image from server. I works for fetching 5-7 images after that got the following:
06-12 16:35:50.904:
WARN/AsyncTask(717):
java.lang.InterruptedException 06-12
16:35:50.904: WARN/AsyncTask(717):
at
java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1254)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:219)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask.get(FutureTask.java:82)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
android.os.AsyncTask$3.done(AsyncTask.java:196)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask$Sync.innerCancel(FutureTask.java:293)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask.cancel(FutureTask.java:75)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
android.os.AsyncTask.cancel(AsyncTask.java:325)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
us.bnn.news.IncidentDetailsActivity$GetImages.doInBackground(IncidentDetailsActivity.java:1691)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
us.bnn.news.IncidentDetailsActivity$GetImages.doInBackground(IncidentDetailsActivity.java:1)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
android.os.AsyncTask$2.call(AsyncTask.java:185)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-12 16:35:50.904:
WARN/AsyncTask(717): at
java.lang.Thread.run(Thread.java:1096)
I an using this method:
public static Bitmap getLargeImage(String imgUrl)
{
Bitmap bitmap = null;
InputStream in = null;
Log.v(TAG,"start fetchimg main images");
try
{
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet(imgUrl);
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = mHttpResponse.getEntity();
if ( entity != null)
{
in = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(in, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
if(in != null)
in.close();
}
}
}
catch (Exception ex)
{
Log.v("Error Fetching images from url",ex.getMessage());
}
return bitmap;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在不同线程中接收图像?如果是,您使用哪个类来下载图像?标准 HttpClient 不是线程安全的,您应该使用 ThreadSafeClientConnManager 来获得线程安全的。
Do you receive images in different threads? If yes, which class do you use to download the images? Standard HttpClient is not thread-safe, you should use ThreadSafeClientConnManager to get thread-safe one.