【问题标题】:Android string file to imageAndroid字符串文件到图像
【发布时间】:2014-10-15 08:35:27
【问题描述】:

正在从服务器端向我发送图像。 我以字符串形式接收的图像,服务器端发送给我的是整个文件。 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

此代码运行产生此日志猫

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

【问题讨论】:

  • 是 base64 还是其他格式?
  • 是的,它实际上是一个文件。我得到的输出是一个文件。
  • 如果我这样做字节 [] encodeByte=Base64.decode(output.toString(),Base64.DEFAULT);我得到错误的 base 64 作为错误
  • 在你的代码中使用这个 base64 类并遵循解码器和编码器的方法。它会工作stackoverflow.com/a/22601645/964741
  • 如何下载文件?你使用哪个库?你能显示更多代码吗?

标签: java android


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2011-10-27
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
相关资源
最近更新 更多