Android 上的间歇性 HttpClient GET 问题
我有一个android应用程序,我一直在开发它从服务器下载图像,将其读入位图并将其显示在ImageView上,
这在大多数情况下都很好用,但每隔一段时间,它就会经历这个过程(有一个 ProgressDialog 说“正在获取图像...”),一旦完成,就不会显示任何内容。 logcat 中似乎没有任何与此相关的内容。
代码如下:
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();
即使没有显示任何内容,Resp 内容长度也始终报告正确的大小,但最终仍然没有显示任何内容。
此代码是从 AsyncTask 调用的,但一次仅调用 1 个任务。
这让我发疯,我不知道为什么它一直这样做。
编辑:这是设置 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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有向我们展示用于显示图像的代码,因此我们不确定该代码是否正确。或许问题就出在那里?
但假设问题是图像已损坏,我将开始调试此问题:用 PushbackInputStream。从buf中读取字节并将其保存到文件中;然后将这些相同的字节推回到 PushbackInputStream 中。然后将
PushbackInputStream
传递到BitmapFactory.decodeStream
中。如果图像成功显示,则删除该文件(手动或以编程方式)。否则,您现在可以随意检查位图。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 thePushbackInputStream
. Then pass thePushbackInputStream
intoBitmapFactory.decodeStream
. If the image is displayed successfully, then delete the file (manually or programatically.) Otherwise, you can now examine the bitmap at your leisure.