如何将Bitmap保存为原始方向的Jpg图像?

时间:2015-11-06 08:24:29

标签: android bitmap

我正在尝试将位图保存为存储中的.Jpg。它使用此方法成功保存。

private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Dir");    

String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

}

但保存的图像方向是90度时钟。我想用原始方向保存它。我该怎么解决这个问题。请建议我。谢谢。

2 个答案:

答案 0 :(得分:2)

你可以试试这个。

首先获取原始图像的Exif数据

ExifInterface originalImageExif = new ExifInterface(origFile.getAbsolutePath());
String origImageOrientation = originalImageExif.getAttribute(ExifInterface.TAG_ORIENTATION);

然后为新图像创建新的Exif数据。

ExifInterface exifForNewImage = new ExifInterface(newFile.getAbsolutePath());

//Pass the origImageOrientation value that you get from the original image
exifForNewImage.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation);

//Then save the Exif attributes
exifForNewImage.saveAttributes();

答案 1 :(得分:1)

我希望这会对你有所帮助。

Bitmap asd = Your bitmap image;
    boolean deleted = false;
    try
    {//You can delete here
        File file = new File("/sdcard/test.png");
        deleted = file.delete();
    }
    catch (Exception e)
    {

    }
    OutputStream stream = null;
    try {
        stream = new FileOutputStream("/sdcard/test.png");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    asd.compress(Bitmap.CompressFormat.PNG, 100, stream);