Exception during fetching the image from webserver

2019-08-05 12:17发布

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

1条回答
Melony?
2楼-- · 2019-08-05 12:29

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.

查看更多
登录 后发表回答