【问题标题】:Android: How can I save image files that are not (JPEG & PNG)? [after rotate]Android:如何保存不是(JPEG 和 PNG)的图像文件? [旋转后]
【发布时间】:2011-11-24 16:17:38
【问题描述】:

我正在尝试从 sdcard 旋转图像,然后保存回 sdcard。

我可以使用 ExifInterface 类为“.jpg”格式做到这一点:

exif = new ExifInterface(filepath);
exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(orientation));
exif.saveAttributes();

对于“.png”文件,我必须实际旋转并保存:

Bitmap bitmap = BitmapFactory.decodeFile(filepath);
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
FileOutputStream stream = new FileOutputStream(fileLocation);
bitmap.compress(CompressFormat.PNG, 100, stream);

“.bmp”、“.tiff”、“.gif”呢??

好像 CompressFormat 只支持 'CompressFormat.PNG' 和 'CompressFormat.JPG'。

这是限制吗?

【问题讨论】:

    标签: android image save rotation bmp


    【解决方案1】:
    【解决方案2】:

    嘿,只需将名称命名为 .bmp

    这样做:

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    _bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
    
    //you can create a new file name "test.BMP" in sdcard folder.
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "**test.bmp**")
    

    听起来我只是在胡闹,但一旦它保存在 bmp 格式中就试试吧..干杯

    【讨论】:

    • 文件扩展名只是一个约定。它将PNG图像保存为test.bmp,但不会影响压缩。我什至不费心在图像缓存中添加文件扩展名 - 使用哈希作为文件名以及最适合的压缩方式。
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2014-01-20
    • 2017-04-16
    • 1970-01-01
    • 2016-11-17
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多