以错误的方向存储图像

时间:2014-12-15 14:22:36

标签: android

以纵向模式拍摄的图像将以横向方式保存。但是在横向模式下拍摄的图像会正确保存。

private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
    @Override
    protected Void doInBackground(byte[]... data) {

        FileOutputStream outStream = null;
        int rotate = 0;
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/SELFie");
            dir.mkdirs();
            String fileName = String.format("%d.jpg", System.currentTimeMillis());

            // Before saving to the file rotate to portrait and save it in a right ratio.

            File outFile = new File(dir, fileName);
            Log.d("SaveImageTask","It is a OutPut media file");
            outStream = new FileOutputStream(String.valueOf(outFile));
            Log.d("SaveImageTask","OutStream");

            // writing data to the file

            outStream.write(data[0]);
            Log.d("SaveImageTask","writing the data to the outStream");
            outStream.flush();

            Log.d("SaveImageTask","OutStream.flush");
            outStream.close();

              Log.d("SaveImageTask","closing the outStream");
            Log.d(DEBUG_TAG, "onPictureTaken - wrote bytes: " + data.length + " to " +       outFile.getAbsolutePath());

             // Refreshing the gallery to save recently taken photos.

             refreshGallery(outFile);

             // After calling the refreshGallery method.

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

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

        } finally {
        }
        return null;

        // Finally returning the null.
    }
}

1 个答案:

答案 0 :(得分:3)

某些Android相机硬件会以横向方式创建纵向图像,JPEG文件中带有EXIF标题,表示图像查看器应旋转图像。

某些Android相机硬件会将肖像图像创建为肖像。

你无法真正做到这一点。我们欢迎您扫描生成的JPEG格式的EXIF标题并自行旋转图像,但这很慢且占用大量内存。