Android:如何保存非(JPEG 和 PNG)的图像文件? [旋转后]

发布于 12-17 15:34 字数 744 浏览 3 评论 0原文

我正在尝试旋转 SD 卡中的图像,然后保存回 SD 卡。

我可以使用 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”。

这是限制吗?

I am trying to rotate an image from sdcard and then save back to sdcard.

I can do that for ".jpg" format by using ExifInterface class:

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

For ".png" files, I would have to actually rotate and save:

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);

What about ".bmp", ".tiff", ".gif" ??

It seems like CompressFormat only supports 'CompressFormat.PNG' and 'CompressFormat.JPG'.

Is this limitation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社2024-12-24 15:34:09

嘿,给 .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 格式保存..干杯

Hey just give the name to .bmp

Do this:

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**")

it'll sound that IM JUST FOOLING AROUND but try it once it'll get saved in bmp foramt..Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文