【问题标题】:How to convert a OutputStream object to a file object?如何将 OutputStream 对象转换为文件对象?
【发布时间】:2018-10-04 11:50:15
【问题描述】:

我需要转换一个图像文件(大尺寸到小尺寸)。所以,我将图像文件解码为位图图像,然后我压缩位图图像。我需要再次将此位图保存到 File 对象中。谁能帮帮我?

【问题讨论】:

  • 您好!欢迎来到 SO!你都尝试了些什么?请提供给我们一些MCVE,以便我们为您提供帮助。
  • 您的图像文件是什么样的?你目前在纠结什么?您可以发布您当前的代码吗?如果没有更多细节,就无法回答这个问题——而且,表明你已经努力解决这个问题,对于让其他人帮助你大有帮助。

标签: java file file-io fileoutputstream


【解决方案1】:

首先将您的位图转换为 byte[] 对象

    Bitmap bmp; //your bitmap object
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

然后创建一个 FileOutputStream 对象并将 byte[] 写入文件输出流

    File yourOutputFile = new File("/the/path/to/your/file")
    FileOutputStream fos = new FileOutputStream(yourOutputFile);
    fos.write(byteArray);
    fos.close();

    return yourOutputFile;//this will be your output

【讨论】:

  • 你能帮忙吗,我需要作为文件对象的输出。
猜你喜欢
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多