2014-02-20 32 views
1

遇到的情况,该httpentity在InputStream的一个图像的二进制数据,处理还其一个库文件[String str = EntityUtils.toString(httpResponse.getEntity())]被转换为字符串,现在我试图让输入流从那个String返回。如何回到从字符串取得InputStream的

采取以下场景了解这一问题:

工作 - 的ImageView与内容显示

InputStream inStream = getContentResolver().openInputStream(thisPhotoUri); 
Bitmap bm = BitmapFactory.decodeStream(inStream); 
ImageView view = (ImageView)findViewById(R.id.picture_frame); 
view.setImageBitmap(bm); 

问题 - ImageView的不与图像显示

InputStream inStream = getContentResolver().openInputStream(thisPhotoUri); 
String str = inStream.toString(); 
InputStream is = new ByteArrayInputStream(str.getBytes()); 
Bitmap bm = BitmapFactory.decodeStream(is); 
ImageView view = (ImageView)findViewById(R.id.picture_frame); 
view.setImageBitmap(bm); 

回答

-1

这应该是什么您正在寻找:

InputStream stream = new ByteArrayInputStream(yourString.getBytes("UTF-8"));

+0

试过这个,它没有帮助。由于Bitmapfactory在传递流时返回null。 – Dinesh

1

InputStream.toString()不这样做,你期待什么。它会调用Object.toString()方法,你会得到类似[email protected]的东西,而不是流的真实内容!

尝试System.out.println(str);看,那是什么价值。

这就是为什么你不能从这个内容再生原始InputStream,因为它不是InputStream内容

您必须阅读以另一种方式流获取内容的String!请参阅:Read/convert an InputStream to a String

1

不能直接InputStream的转换为字符串。这可能是问题。

String str = inStream.toString(); 

Take a look at this确定将InputStream转换为String的方式。

+0

尝试链接中提到的字符串转换,当我将字符串转换回流并传递给位图工厂时,它仍然返回null。 – Dinesh

+0

为什么你想要将输入流转换为字符串,你对它做了什么修改? – Rakhita

+0

Http调用是在jar文件中完成的,响应以json的形式返回,因此在这种情况下,流(image binary)被转换为字符串并添加到json中,应用程序层现在试图使用该字符串来获取流返回并获取图像。 – Dinesh

相关问题