Намерение камеры / галереи дает изображения с необычными размерами - PullRequest
0 голосов
/ 31 марта 2020

Я пытаюсь получить фотографии как с моей камеры, так и из моей галереи.

Проблема, с которой я сталкиваюсь, заключается в том, что фотография, которую я получаю с камеры, не заполняет весь просмотр изображения -

enter image description here

, и изображение, которое я получаю из галереи, является пейзажным, а не портретным -

enter image description here

Вот мои намерения как для камеры и галереи, так и для метода onActivityResult() -

val dialog = AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog_Alert).apply {
            setMessage(getString(R.string.profile_activity_select_an_option))
            setPositiveButton(getString(R.string.profile_activity_camera)) { _, _ ->
                selectedProfileLocation = ProfilePictureLocation.CAMERA
                if (hasCameraPermission()) {
                    takePhotoFromCamera()
                    return@setPositiveButton
                }
                requestPermission()
            }
            setNegativeButton(getString(R.string.profile_activity_gallery)) { _, _ ->
                selectedProfileLocation = ProfilePictureLocation.GALLERY
                val intent = Intent(Intent.ACTION_PICK)
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Constants.profileImageType)
                startActivityForResult(intent, Constants.profilePhotoSelectedCode)
            }
            setNeutralButton(getString(R.string.profile_activity_delete_photo), null)
        }.create()
        dialog.show()
private fun takePhotoFromCamera() {
        Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
            takePictureIntent.resolveActivity(packageManager)?.also {
                startActivityForResult(takePictureIntent, Constants.profilePhotoSelectedCode)
            }
        }
    }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //Gallery pick -
        if (resultCode != Activity.RESULT_OK || requestCode != Constants.profilePhotoSelectedCode) return

        didUserUploadProfilePicture = true
        if (selectedProfileLocation == ProfilePictureLocation.CAMERA) {
            if (requestCode == Constants.profilePhotoSelectedCode && resultCode == RESULT_OK) {
                val imageBitmap = data?.extras?.get("data") as Bitmap
                profileImage.setImageBitmap(imageBitmap)
            }
            return
        }


        val imageUri = data?.data
        profileImage.setImageURI(imageUri)
        try {
            val inputStream: InputStream = contentResolver?.openInputStream(imageUri!!)!!
            val bitmap = BitmapFactory.decodeStream(inputStream)
            setPic(profileImage, bitmap)
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }
    }
 private fun setPic(imageView: ImageView, bitmap: Bitmap) {
        //bitmap values
        val bitmapWidth = bitmap.width
        val bitmapHeight = bitmap.height
        //Imageview value
        val imageViewHeight = imageView.height
        val imageViewWidth = imageView.width
        //scaling values
        val scaleWidth = bitmapWidth.coerceAtMost(imageViewWidth)
        val scaleHeight = bitmapHeight.coerceAtMost(imageViewHeight)
        val scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true)
        //        ImageUtils.rotateBitmap(scaledBitmap, 90f)
        imageView.setImageBitmap(scaledBitmap)
        try {
            val filename = Constants.profileImagePrefix
            val file = getExternalFilesDir(null)?.absolutePath;
            val destination = File(file, filename)
            val fileOutputStream = FileOutputStream(destination)
            scaledBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
            cachedLocalImageUri = Uri.fromFile(destination)
            fileOutputStream.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

1) Как повернуть изображение с камбуза?

2) Как сделать так, чтобы изображение с камеры заполняло весь ImageView?

...