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