从android中的webserver获取图像时出现异常

时间:2011-06-12 11:11:10

标签: android webserver android-asynctask imagesource

您好,我从服务器获取图像时遇到以下异常。在获得以下内容之后,我致力于获取5-7张图像:

  

06-12 16:35:50.904:   WARN /的AsyncTask(717):   java.lang.InterruptedException 06-12   16:35:50.904:WARN / AsyncTask(717):
  在   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;
    }

1 个答案:

答案 0 :(得分:1)

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