【问题标题】:RecyclerView take Image form camera is rotating when show on the listRecyclerView 在列表中显示时从相机获取图像正在旋转
【发布时间】:2019-05-18 21:17:18
【问题描述】:

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

有人可以帮助我吗?

代码如下:

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

如果相机选择拍照

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);

如果画廊选择拍照

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();

   }
  }
}

  }
}

【问题讨论】:

标签: java android camera gallery


【解决方案1】:

相机始终根据相机方向为您提供图片。我试过下面的代码来改变方向

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
    }

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多