Android的/爪哇 - 如何从Web服务中检索图像并显示它 [英] Android/Java - How to retrieve image from web service and show it
问题描述
我使用一个Web服务通常返回JSON阶跃响应,而是一个服务的一些服务,当我发送用户的ID返回一个静态的GIF图像(非动态)。
I'm using some services of a web service which usually returns JSON respones, but one service when I send the ID of a user returns a static GIF image (Not animated).
这是我做的程序是:
使用1.连接到Web服务DefaultHttpClient结果
2.Convert InputStream的接收到字符串与此utilitary方法:
1.Connect to web service using DefaultHttpClient
2.Convert the InputStream received to a String with this utilitary method:
public static String inputStreamToStringScanner(InputStream in) {
Scanner fileScanner = new Scanner(in);
StringBuilder inputStreamString = new StringBuilder();
while(fileScanner.hasNextLine())
inputStreamString.append(fileScanner.nextLine()).append("\n");
fileScanner.close();
return inputStreamString.toString();
}
3.Storage转换后收到的字串来处理服务器响应。
3.Storage the converted received String to process the server response.
有关影像服务,当我看到转换的字符串,它是这样开始的:
的GIF89a ?? ......
For the Image Service, when I see the string converted, it begins like this: "GIF89a?��?�� ..."
这是一个静态的GIF文件。
It's a static GIF file.
我不能够显示在ImageView的形象,我已经试过,我在网上找到不同的东西:
I'm not being able to show the image in a ImageView, I have tried different things that I found in the web:
public void onPhotoFinished (String responseData) {
InputStream is = new ByteArrayInputStream(responseData.getBytes());
Bitmap bm = BitmapFactory.decodeStream(is);
userImage.setImageBitmap(bm);
}
这是别的东西,我自己也尝试:
This is something else that I have also tried:
public void onPhotoFinished (String responseData) {
InputStream is = new ByteArrayInputStream(responseData.getBytes());
final Bitmap bm = BitmapFactory.decodeStream(new BufferedInputStream(is));
userImage.setImageBitmap(bm);
}
这也没有工作:
public void onPhotoFinished (String responseData) {
InputStream is = new ByteArrayInputStream(responseData.getBytes());
Drawable d = Drawable.createFromStream(is, "src name");
userImage.setImageDrawable(d);
}
最后,这并不工作之一:
Finally, this doesn't work either:
public void onPhotoFinished (String responseData) {
Bitmap bm = BitmapFactory.decodeByteArray(responseData.getBytes(), 0, responseData.getBytes().length);
userImage.setImageBitmap(bm);
}
在LogCat中我收到去codeR->德code返回false
In Logcat I receive "decoder->decode returned false"
似乎没有任何工作...的什么是错的任何想法?
Nothing seems to work... any ideas of what is wrong?
谢谢!
推荐答案
最后,我FlushedInputStream并使用直接输入流解决它,避免了转换为字符串:
Finally, I resolved it with FlushedInputStream and using directly the Input Stream, avoiding the conversion to String:
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int byteReaded = read();
if (byteReaded < 0) {
break;
} else {
bytesSkipped = 1;
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
和
Bitmap bitmapResponseData = BitmapFactory.decodeStream(new FlushedInputStream(is));
问候
这篇关于Android的/爪哇 - 如何从Web服务中检索图像并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!