RecyclerView take Image from camera 在显示在列表中时正在旋转

RecyclerView take Image form camera is rotating when show on the list

我的代码有问题。当我从我的应用程序拍摄照片时,它会旋转 90º 来显示。但是,当我为 select 我的照片选择画廊选项时,它显示正常。

有人可以帮帮我吗??

代码如下:

Adapter.java

public void onBindViewHolder(@NonNull final ProdutosListaViewHolder holder, final int i){


Produto prod = produtos.get(i);
File foto = mPicture.getImageProduto(prod.codProd);
if(foto == null || foto.exists()){
holder.foto.setImageResource(R.drawable.sem_foto_icon);
}else{
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inPreferredConfig = Bitmap.Config.RGB_565;
   Bitmap bitmap = BitmapFactory.decodeFile(foto.getAbsolutePath(), options);
   holder.foto.setImageBitmap(bitmap);
}

}

PictureManager.java

如果相机select可以拍照

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(ctx, ....));
((Activity) ctx).startActivityForResult(i, 999);

如果画廊select允许拍照

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity) ctx).startActivityForResult(i, 998);

ProductListActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data){
   super.onActivityResult(requestCode, resultCode, data);
   if(requestCode==999){
    try{
     File foto = (new PictureManager(FOTO_PRODUTO, ctx)).getImageProduto(produtoSelecionado.codProd);
    if(foto != null){
    mProdutosListaAdapter.notifyItemChanged(posicaoSelecionada);
   }
}catch(Exception e){
 e.printStackTrace();
 }
}else if(requestCode==998){
  try{
   Uri selectedImage = data.getData();
   String[] filePathColumn = { MediaStore.Images.Media.DATA };
   Cursor cursor = ctx.getContentResolver().query(selectedImage, filePathColumn, null null, null);
   cursor.moveToFirst();
   int columnIndex = cursor.ColumnIndex(filePathColumn[0]);
   String picturePath = cursor.getString(columnIndex);
   cursor.close();
   File file = new File(picturePath);
   int codigo = produtoSelecionado.codProd;

   File foto = (new PictureManager(FOTO_PRODUTO, ctx)).salvarFoto(file, codigo, 1);
   mProdutosListaAdapter.notifyDataSetChanged();

   }
  }
}

  }
}

相机总是根据相机方向给你照片。我试过下面的代码来改变方向

public static Bitmap rotateImageOrientation(String photoFilePath) {
        // Create and configure BitmapFactory
        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoFilePath, bounds);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        Bitmap bm = BitmapFactory.decodeFile(photoFilePath, opts);
        // Read EXIF Data
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(photoFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
        int rotationAngle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
        // Rotate Bitmap
        Matrix matrix = new Matrix();
        matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        return Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
        // Return result
    }