Android字符串文件到图像

时间:2014-10-15 08:35:28

标签: java android

从服务器端向我发送图像。 我收到的图像是字符串,服务器端发送给我的是整个文件。 我如何在android端读取它?

File tempFile = File.createTempFile("image", ".jpg", null); 
// tempFile.wr
byte[] bytes = output.toString().getBytes();
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bytes);

输出是我收到的结果,它的字符串是从服务器端发送的整个文件。

Bitmap myBitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
m2.setImageBitmap(myBitmap);

我得到SkImageDecoder :: Factory返回null

此代码运行生成此日志cat

File tempFile = File.createTempFile("image", ".jpg", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(output.toString().getBytes());
fos.close();  
System.out.println(""+tempFile.getAbsolutePath());
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap  = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
m2.setImageBitmap(bitmap);

http://www.speedyshare.com/8BdVs/log.txt

1 个答案:

答案 0 :(得分:1)

这是从base64解码的图像字符串转换为流的代码。只有在Base64中正确编码并将其存储在您的服务器中

,这才有效
 Bitmap img = null;
 InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
 img = BitmapFactory.decodeStream(stream);