gpt4 book ai didi

android - 在android中从网络服务器获取图像时出现异常

转载 作者:行者123 更新时间:2023-11-30 04:39:06 26 4
gpt4 key购买 nike

您好,我在从服务器获取图像时遇到以下异常。在得到以下结果后,我开始获取 5-7 张图片:

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)

我正在使用这个方法:

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;
}

最佳答案

您是否在不同的线程中接收图像?如果是,您使用哪个类下载图像?标准 HttpClient 不是线程安全的,您应该使用 ThreadSafeClientConnManager 来获得线程安全的。

关于android - 在android中从网络服务器获取图像时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6321610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com