0

通常はJSON応答を返すWebサービスの一部のサービスを使用していますが、ユーザーのIDを送信すると1つのサービスが静的GIF画像(アニメーション化されていない)を返します。

私が行っている手順は次のとおりです。

1.DefaultHttpClientを使用してWebサービスに接続します
。2。次の実用的なメソッドを使用して受信したInputStreamを文字列に変換します。

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.変換された受信文字列を保存して、サーバーの応答を処理します。

Image Serviceの場合、変換された文字列を見ると、「GIF89a?��?��...」のように始まります。

これは静的GIFファイルです。

ImageViewに画像を表示できません。ウェブで見つけたさまざまなことを試しました。

public void onPhotoFinished (String responseData) {

  InputStream is = new ByteArrayInputStream(responseData.getBytes());
  Bitmap bm = BitmapFactory.decodeStream(is);
  userImage.setImageBitmap(bm);
}

これは私も試した他の何かです:

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

最後に、これも機能しません。

public void onPhotoFinished (String responseData) {

  Bitmap bm = BitmapFactory.decodeByteArray(responseData.getBytes(), 0, responseData.getBytes().length);
  userImage.setImageBitmap(bm);
}

Logcatで、「decoder->decodereturnedfalse」を受け取ります

何もうまくいかないようです...何が悪いのかについてのアイデアはありますか?

ありがとう!

4

1 に答える 1

1

最後に、FlushedInputStreamを使用し、入力ストリームを直接使用して、文字列への変換を回避して解決しました。

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

よろしく

于 2012-08-21T17:16:33.570 に答える