1

Why does my image rotate when I save it in Android?

My image rotates when I save it in the external storage of my device.

My code is the following:

public String saveImage(Bitmap myBitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
    File wallpaperDirectory = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/directory");
    // have the object build the directory structure, if needed.
    if (!wallpaperDirectory.exists()) {
        wallpaperDirectory.mkdirs();
    }

    try {
        File f = new File(wallpaperDirectory, Calendar.getInstance()
                .getTimeInMillis()+"_TC" + ".jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        MediaScannerConnection.scanFile(activity,
                new String[]{f.getPath()},
                new String[]{"image/jpeg"}, null);
        fo.close();
        Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());

        return f.getAbsolutePath();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return "";
}

enter image description here

ja12
  • 351
  • 2
  • 16
  • check it - https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a – Adil Mar 09 '18 at 10:59
  • Try this [answer](https://stackoverflow.com/a/17674787/886001) to save your bitmap instead of creating `ByteArrayOutputStream` and a `FileOutputStream` – ColdFire Mar 09 '18 at 11:00
  • We cannot see what kind of original you use. A jpg file? We also do not know how and where you display the original and the 'IMAGE SAVED'. Pretty unclear what you are doing. – greenapps Mar 09 '18 at 11:48

0 Answers0