Android - 将图像垂直发送到服务器时,尽管移动设备中的预览看起来不错,但它们会旋转

标签 android image rotation

发送文件前在手机上预览看起来不错,但一旦在服务器上接收并重新加载到手机上,就会出现旋转的垂直图像。

public static File saveBitmapTemporarily(Bitmap finalBitmap, int extension, ExifInterface oldExif) {
    String root = Environment.getExternalStorageDirectory().toString();

    FileUtils.createFolder(new File(Environment.getExternalStorageDirectory() + BuildConfig.STORAGE_DIR));

    File myDir = new File(root + BuildConfig.STORAGE_DIR + "/");
    myDir.mkdirs();
    String fname;
    if (extension == IMAGE_FORMAT_JPG_JPEG) {
        fname = "file-reduced.jpg";
    } else {
        fname = "file-reduced.png";
    }
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        if (extension == IMAGE_FORMAT_JPG_JPEG) {
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        } else {
            finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }
        out.flush();
        out.close();

    } catch (Exception e) {
        if (BuildConfig.DEBUG)
            e.printStackTrace();
    }

    return file;
}

最佳答案

欢迎来到 Stack Overflow @kiketurry...这里有我为解决这个问题所做的工作。您应该在照片的 exif 数据中存储宽度和高度尺寸,以便服务器知道如何定位它,但它只能是 jpg 格式

if (extension == IMAGE_FORMAT_JPG_JPEG) {
    ExifInterface oldExif = null;
    try {
        oldExif = new ExifInterface(file.getAbsolutePath());
        ExifInterface newExif;
        newExif = new ExifInterface(file.getAbsolutePath());
        newExif.setAttribute("ImageLength", String.valueOf(finalBitmap.getHeight()));
        newExif.setAttribute("ImageWidth", String.valueOf(finalBitmap.getWidth()));
        if (oldExif != null) {
            String exifOrientation = oldExif.getAttribute(ExifInterface.TAG_ORIENTATION);
            if (exifOrientation != null) {
                newExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation);
            }
        }
        newExif.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

希望对你有帮助。

关于Android - 将图像垂直发送到服务器时,尽管移动设备中的预览看起来不错,但它们会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41424087/

相关文章:

android - 为什么android.Matrix.setRotateM返回一个旋转方向与预期旋转矩阵相反的矩阵?

ios - UI 旋转手势识别器捕捉直角

java - 将非 Android 项目添加到 Android 项目

javascript - 页面加载后使用 Ajax 重新检查图像 src

java - .txt 文件上的字符串转换为位图

ios - 如何在 Xcode 中操作 CGImageRef 中的像素值

javascript - 使用 interact.js 拖放

android - 传输数据 USB

android - 在 android 中,当方向改变时,背景图像不会改变

Android 3d 布局旋转动画