1

I am trying to save bitmap as .Jpg in storage. It saved successfully with this method .

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

}

But saved Image orientation is 90 degree clockwised. I want to save it with original orientation. How can I solve this. Please kindly suggest me. Thank you.

Leonar Aung
  • 187
  • 9
  • There are a lot of posts on SO that you could find with a little Google search... see [this question](http://stackoverflow.com/a/11081918/2668136) and [this one](http://stackoverflow.com/questions/25231148/android-camera-saving-images-with-wrong-orientation) for example. – Blo Nov 06 '15 at 08:29
  • This problem specially occurs with some android api device before lollipop when capturing by camera and save or use. They should be rotated accordingly – Androider Nov 06 '15 at 08:45
  • I think I should use shell command to copy it. – Leonar Aung Nov 06 '15 at 08:48

2 Answers2

4

You may try this.

First get the Exif data of the original image

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

Then create new Exif data for the new image.

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();
DarwinLouis
  • 1,111
  • 9
  • 15
1

I hope this will help you.

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);
alican akyol
  • 303
  • 3
  • 14