Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 位图工厂在传递输入流时返回null_Android - Fatal编程技术网

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编码创建,我怀疑您正在用字符串填充它。