Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9414

get the content provider uri crash in android

I have the following file path on my device

file:///storage/emulated/0/DCIM/IMG_20190206_141330.jpg

I am trying to get content provider for it to be able to rotate it using this method in kotlin

private fun Uri.getPath(ctx: Context): String? {
    val projection = arrayOf(MediaStore.Images.Media.DATA)
    val cursor = ctx.contentResolver.query(this, projection, null, null, null) ?: return null
    val columnIndex = cursor.getColumnIndexOrThrow(projection[0])
    cursor.moveToFirst()
    val s = cursor.getString(columnIndex)
    cursor.close()
    return s
}

I am getting the following crash

 java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:198)
        at com.forsale.app.utils.UriExtensionsKt.rotateImageIfRequired(UriExtensions.kt:80)
        at com.forsale.app.features.postad.basicinformation.PostAdBasicInformationFragment$onActivityResult$$inlined$let$lambda$1.invokeSuspend(PostAdBasicInformationFragment.kt:254)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

can anyone help please?

Upvotes: 0

Views: 231

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007359

I am trying to get content provider for it

That's not going to work. A content Uri has content:// as the scheme. Yours has file:// as the scheme.

for it to be able to rotate it

You can get rid of Uri.getPath() (which will not work for most Uri values, even those with content:// schemes). Use openInputStream() on a ContentResovler to open an InputStream on the content identified by the Uri (works for both content:// and file:// Uri values). Then, use that InputStream to read in the image to rotate it.

Upvotes: 1

Related Questions