RecyclerView принять изображение формы камеры вращается, когда шоу в списке - PullRequest
0 голосов
/ 18 декабря 2018

У меня проблема с моим кодом.Когда я беру фотографию из своего приложения, она поворачивается на 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();

   }
  }
}

  }
}

1 Ответ

0 голосов
/ 18 декабря 2018

Камера всегда выдает изображение в соответствии с ориентацией камеры.Я попробовал приведенный ниже код для изменения ориентации

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
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...