Android 位图工厂在传递输入流时返回null
我使用以下代码从位图工厂获取位图,但它返回Android 位图工厂在传递输入流时返回null,android,Android,我使用以下代码从位图工厂获取位图,但它返回null InputStream stream = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)); bitmap = BitmapFactory.decodeStream(stream); 我通过以下代码获得结果字符串: DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams httppar
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();
BitmapFactory.decodeStream
要求传入二进制位图数据。如果输入流为null
,或无法用于解码位图,则函数返回null
InputStream stream = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8));
bitmap = BitmapFactory.decodeStream(stream);
从您的代码中可以清楚地看出,您正在将文本数据传递到BitmapFactory
,这就是失败的原因。在将文本数据传递给BitmapFactory
之前,必须将其解码为位图二进制数据
如何做到这一点取决于位图数据如何在服务器端编码成字符串。为什么要输入这种数据?位图可以从二进制数据和代码中的UTF-8编码创建,我怀疑您正在用字符串填充它。