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

发布于 2024-11-14 20:09:44 字数 3302 浏览 1 评论 0原文

您好,我在从服务器获取图像时遇到以下异常。我在获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-11-21 20:09:44

您是否在不同线程中接收图像?如果是,您使用哪个类来下载图像?标准 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文