0

I have a ExifUtil which rotates the bitmap based on EXIF, this class works fine if the path of the image local on the device.

I want to use the same class or same functionality if the image path is from the AWS. how can I achieve that please.

Thanks in advance R

url is https://xxx-2.amazonaws.com/YRINWoUKBOW97VXvpnR....

class ExifUtil {
    companion object {
        fun rotateBitmap(src: String, bitmap: Bitmap): Bitmap {
            try {
                val orientation: Int = getExifOrientation(src)
                if (orientation == 1) {
                    return bitmap
                }
                val matrix = Matrix()
                when (orientation) {
                    2 -> matrix.setScale(-1f, 1f)
                    3 -> matrix.setRotate(180f)
                    4 -> {
                        matrix.setRotate(180f)
                        matrix.postScale(-1f, 1f)
                    }
                    5 -> {
                        matrix.setRotate(90f)
                        matrix.postScale(-1f, 1f)
                    }
                    6 -> matrix.setRotate(90f)
                    7 -> {
                        matrix.setRotate(-90f)
                        matrix.postScale(-1f, 1f)
                    }
                    8 -> matrix.setRotate(-90f)
                    else -> return bitmap
                }
                return try {
                    val oriented =
                        Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
                    bitmap.recycle()
                    oriented
                } catch (e: OutOfMemoryError) {
                    e.printStackTrace()
                    bitmap
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }
            return bitmap
        }

        @Throws(IOException::class)
        private fun getExifOrientation(src: String): Int {
            var orientation = 1
            try {
                val exif = ExifInterface(src);
                orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    1
                );
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: SecurityException) {
                e.printStackTrace()
            } catch (e: NoSuchMethodException) {
                e.printStackTrace()
            } catch (e: IllegalArgumentException) {
                e.printStackTrace()
            } catch (e: InstantiationException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: InvocationTargetException) {
                e.printStackTrace()
            } catch (e: NoSuchFieldException) {
                e.printStackTrace()
            }
            return orientation
        }
    }
}
BRDroid
  • 3,920
  • 8
  • 65
  • 143
  • `ExifInterface` has [a constructor that takes an `InputStream`](https://developer.android.com/reference/androidx/exifinterface/media/ExifInterface#ExifInterface(java.io.InputStream)), though they recommend not using that for network I/O. And, to avoid downloading the image twice, you may want to use an image-loading library that can handle the orientation change for you, or at least download the image yourself before use. – CommonsWare Feb 21 '22 at 17:04
  • Hello I tried using picasso but may be I did not handle the orientation – BRDroid Feb 21 '22 at 17:06
  • `though they recommend not using that for network I/O. ` I found it not working from a network input stream and i wonder what one has to do if code works for an inputstream to make it not work from a network inputstream. I solved the problem by reading the network stream and put all the bytes in a ByteArrayOutputSteam. After that exif interface could read from the baos and BitmapFactory could make a bitmap from it. So just one download @CommonsWare. – blackapps Feb 21 '22 at 18:06

0 Answers0