如何在android相机预览应用程序中旋转图像,然后再保存?

如何在android相机预览应用程序中旋转图像,然后再保存?,android,android-camera,Android,Android Camera,我有一个Android摄像头应用程序,它是在肖像模式下拍摄的,摄像头预览很好。。。但当我拍照时,它保存在横向模式下。。。旋转90度:这是我在PictureTaken()上的代码: 可以将位图旋转90度,如下所示: Matrix matrix = new Matrix(); matrix.postRotate(90); Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix

我有一个Android摄像头应用程序,它是在肖像模式下拍摄的,摄像头预览很好。。。但当我拍照时,它保存在横向模式下。。。旋转90度:这是我在PictureTaken()上的代码:


可以将位图旋转90度,如下所示:

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
在特定示例中,需要将其保存回原始路径:

Bitmap bm = BitmapFactory.decodeFile(filename);

Matrix matrix = new Matrix();
matrix.postRotate(90);    
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

FileOutputStream fos = new FileOutputStream(pictureFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();

您也可以使用

它以什么方式不起作用?它是否抛出异常?或者只是不旋转?是否将compress()调用更改为rotatedBitmap.compress(Bitmap.CompressFormat.JPEG,90,bao)?是的,我已经调用了rotatedBitmap.compress(Bitmap.CompressFormat.JPEG,90,bao)。。但它只是没有旋转!!好的,您的代码正在将位图写入base64字符串,我假设这就是您想要的。请参阅编辑以回答如何保存旋转的JPEG文件。我尝试了此操作,但其抛出异常01-07 02:56:37.736:E/BitmapFactory(17914):无法解码流:java.io.FileNotFoundException:/storage/emulated/0/DCIM/Camera/Picture\u 20155607025637.jpg:open失败:eNot(没有这样的文件或目录)。。
Bitmap bm = BitmapFactory.decodeFile(filename);

Matrix matrix = new Matrix();
matrix.postRotate(90);    
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

FileOutputStream fos = new FileOutputStream(pictureFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();