位图工厂传递输入流时返回null

时间:2015-06-09 07:59:24

标签: android

我使用以下代码从Bitmap工厂获取Bitmap,但它返回null

InputStream stream = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8));
bitmap = BitmapFactory.decodeStream(stream);

我按照以下代码获取结果字符串:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams httpparams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpparams, 60000);
HttpConnectionParams.setSoTimeout(httpparams, 60000);

HttpPost httpPost = new HttpPost(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
/*
 * Reading the response from the Server
 */

BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}
inputStream.close();

responseData = sb.toString();

1 个答案:

答案 0 :(得分:0)

BitmapFactory.decodeStream期望传入二进制位图数据。如果输入流是null,或者不能用于解码位图,则函数返回null

从您的代码中可以清楚地看到,您正在将文本数据传递到BitmapFactory,这就是失败的原因。在将文本数据传递给BitmapFactory之前,必须将其解码为位图二进制数据。

如何做到这一点取决于位图数据如何编码到服务器端的字符串中。