how to resolve java.io.FileNotFoundException when trying to get inputstream from AWS url

915 views Asked by At

my main goal is to get the orientation of a image which I get from AWS, part of the code is to first get the imputStream for which I need to pass the URI, I am doing Uri.parse(url) to get the uri and when I try to get URI I get

java.io.FileNotFoundException: No content provider

URL passed:

https://xxx-2.amazonaws.com/YRINWoUKBOW97VXvpnRelWioT2r2/6d95054e-8a6c-4a61-a4ff-18f63aa187b4/1/thumbnail.png?X-Amz-Algorithm=AWS4-HMAC-xx&X-Amz-Date=20220221T203050Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1800&X-Amz-Credential=AKIAJGEUAZB7E5XX6NPA%2F20220221%2Feu-west-2%2Fs3%xx&X-Amz-Signature=d3839d925efc6fffff417e25af1e6bad28ed10ef27d1c4460d20470

error

java.io.FileNotFoundException: No content provider: https://xxx2.amazonaws.com/YRINWoUKBOW97VXvpnRelWioT2r2/6d95054e-8a6c-4a61-a4ff-18f63aa187b4/1/thumbnail.png?X-Amz-Algorithm=AWS4-HMAC-xxx&X-Amz-Date=20220221T203050Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1800&X-Amz-Credential=AKIAJGEUAZB7E5XX6NPA%2F20220221%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Signature=d3839dffff925efc6417e25aff414d76b8ed10ef27d1c4460d20470

this is my code

private fun getExifOrientationFromURI(context: Context, url: String): Int {
        var orientation: Int = 1
        try {
    context.contentResolver.openInputStream(Uri.parse(url)).use { inputStream -> //ERROR IS AT THIS LINE 
                if(inputStream == null) return  orientation
                val exif = ExifInterface(inputStream)
                orientation =
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return orientation
    }

what am I doing wrong?

1

There are 1 answers

6
CommonsWare On BEST ANSWER

openInputStream() on ContentResolver does not support http or https URLs, only file, content, and android.resource Uri values. You need to use an HTTPS API for your URL, such as OkHttp.