android - 如何将 OutputStream 转换为文件?

下面的代码从某个位置获取图像 - 并使该图像压缩图像。 但是我需要将压缩图像作为文件...我该怎么做?

   File compressFile = null;
   File file = new File("MyFile");
   OutputStream fOut    = new FileOutputStream(file);
   if(image.compress(Bitmap.CompressFormat.PNG, 100, fOut))
   {
       fOut.flush();
       fOut.close();

      compressFile = ??? // I need here to make the OutputStream  to be back to file. 
   }

最佳答案

image.compress 已经将图像写入文件,假设您提供了正确的文件名。 所以像这样使用它:

   File file = new File(Environment.getExternalStorageDirectory() + "/myimage.png");
   FileOutputStream fOut = new FileOutputStream(file);
   image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
   fOut.close();

https://stackoverflow.com/questions/8296248/

相关文章:

sql - 使用 SQL 脚本创建 SQL Server 数据库

assembly - 将二进制转换为十进制并在汇编中显示

php - 在表单中发布非输入字段?

git - 当我用分支推送时,.gitignore 不会忽略文件

php - 我可以将匿名函数放在 PHP 的数组中吗?

vb.net - 从 ListView 中删除项目

sql-server - SQL Server 2008,我可以在 udf 中的 select 语句

qt - 如何使用QT从打印机打印图像文件

c# - 如何从后面的 C# 代码更新 WPF 绑定(bind)的值?

visual-studio - VS2010 中缺少“属性页”选项卡