Android:我如何保存非JPEG和PNG格式的图像文件?[轮换后]

Android:我如何保存非JPEG和PNG格式的图像文件?[轮换后],android,image,save,rotation,bmp,Android,Image,Save,Rotation,Bmp,我正在尝试旋转SD卡中的图像,然后将其保存回SD卡 我可以使用ExiFinInterface类为“.jpg”格式执行此操作: exif = new ExifInterface(filepath); exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(orientation)); exif.saveAttributes(); 对于“.png”文件,我必须实际旋转并保存: Bitmap bitmap = BitmapF

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

我可以使用ExiFinInterface类为“.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”


这是限制吗?

是的,仅限于JPG、PNG、WEBP


嘿,给.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 foramt中,请尝试一下。干杯

文件扩展名只是一种惯例。它会将PNG图像保存为test.bmp,但不会影响压缩。我甚至不想在图像缓存中添加文件扩展名——使用哈希作为文件名和任何最适合的压缩。