This question has been asked countless times, but none of them actually fix the rotation issue when saving a Bitmap
.
This is how I initially saved a Bitmap
to my device:
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mStringFilePath);
//Time is us
int mPresentationTime = mPlayer.getPresentationTime();
Bitmap mBitmap = mmr.getFrameAtTime(mPresentationTime, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
File mFileBitmap = new File(directoryToStore, "test.png");
try {
FileOutputStream outputStream = new FileOutputStream(mFileBitmap);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
The above saves the .png
with the wrong orientation.
I then saw this answer, but the problem is that it rotates the, already saved, Bitmap
to the correct orientation. This is fine if you want to set the Bitmap
to a ImageView
, for example. But, what if I want to share the Bitmap
, or want to open it in the devices Gallery, the orientation would then still be incorrect. I would then have to do the same process as above - FileOutputStream
etc. This will then just cause the same issue.
How can I save a Bitmap
to the device with the correct orientation?
Edit 1
Trying the answer provided by Long.
I created a new class called RotateBit
, with the 2 method you provided. I then changed my code to this:
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mStringFilePath);
//Time is us
int mPresentationTime = mPlayer.getPresentationTime();
Bitmap mBitmapBeforeRotation = mmr.getFrameAtTime(mPresentationTime, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
int rotatingInt = RotateBit.getBitmapOriention(mStringFilePath);
Bitmap mBitmap = RotateBit.rotateBitmap(mBitmapBeforeRotation, rotatingInt);
File mFileBitmap = new File(directoryToStore, "test.png");
try {
FileOutputStream outputStream = new FileOutputStream(mFileBitmap);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
But still the rotation is incorrect.
Edit 2
I noticed that this issue is related to FFmpegMediaMetadataRetriever
, when using MediaMetadataRetriever
this issue doesn't happen.