开发者

Intermittent HttpClient GET issue on android

I have an android application i have been working on that downloads an image from a server, reads it into a bitmap and displays it on an ImageView

This works great most of the time, but every so often, it goes through the process (There is a ProgressDialog saying "Fetching image...") and once its done nothing gets displayed. There has not been anything in logcat that even seems to remotely relate to this.

Here is the code:

    Bitmap image = null;

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(webService + "?cmd=get");

    try
    {
        HttpResponse resp = client.execute(get);

        Log.i("PhotoRouletteDebug", "Resp buffer size: " + (int)resp.getEntity().getContentLength());
        InputStream is = resp.getEntity().getContent();

        BufferedInputStream buf = new BufferedInputStream(is, (int)resp.getEntity().getContentLength());

        image = BitmapFactory.decodeStream(buf);

        // clean up
        buf.close();
        is.close();

Even when nothing is getting displayed, the Resp content length always reports a correct size but still, nothing ends up getting displayed.

This code is called from an AsyncTask, but only 1 task is ever called at a time.

This is driving me insane, i have no idea why its keeps doing this.

Edit: Here is the code that sets the imageView

// AsyncTask for Getting a new image from the queue
protected class GetImageTask extends AsyncTask<String, String, Bitmap>
{
    protected void onPreExecute()
    {
        // lets show a progress dialog so the user knows something is going on
        progressDialog = ProgressDialog.show(PhotoRoulette.this, "", "Fetching image...", true);
    }

    protected void onPostExecute (Bitmap image)
    {
        // we got a new photo so lets display it where it needs to be displayed
        try
        {
            photoView = (ImageView)findViewById(R.id.photoView);
            photoView.setImageBitmap(image);
        }
        catch 开发者_开发知识库(Exception e)
        {
            Log.e("Debug", "Something absolutely retarded happened", e);
        }

        // hide the progress dialog - we're all done
        progressDialog.dismiss();
    }

    protected Bitmap doInBackground(String... urls)
    {
        // Get a new Bitmap Queue Image
        Bitmap image = imageHandler.getQueueImage();
        return image;
    }
}


You didn't show us the code for displaying the image, so we don't know for sure that that code is correct. Perhaps the problem lies there?

But assuming that the problem is that the image is getting corrupted, here's how I'd start debugging this: Wrap buf with a PushbackInputStream. Read the bytes out of buf and save them to a file; then push those same bytes back into the PushbackInputStream. Then pass the PushbackInputStream into BitmapFactory.decodeStream. If the image is displayed successfully, then delete the file (manually or programatically.) Otherwise, you can now examine the bitmap at your leisure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜