Android:如何保存非(JPEG 和 PNG)的图像文件? [旋转后]
我正在尝试旋转 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 技术交流群。

发布评论
评论(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 格式保存..干杯
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是 仅限 JPG 、 PNG 、 WEBP
http://developer.android.com /reference/android/graphics/Bitmap.CompressFormat.html
Yes Limited To JPG , PNG , WEBP
http://developer.android.com/reference/android/graphics/Bitmap.CompressFormat.html