java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.String
brookmg opened this issue · 3 comments
brookmg commented
I think this is happening because of the kotlin compiler.
val paths = data?.getStringArrayListExtra(KEY_SELECTED_MEDIA) ?: ArrayList()
paths.forEach { // it becomes String as `getStringArrayListExtra` is meant to return a String array
val uploadable = File((it as? Uri)?.path)
publishFragmentViewModel.uploadableFiles.value?.add(uploadable)
}I guess it's forcefully defining the ArrayList type to be a String and I'm getting
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.machemarket.android, PID: 14004
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=73993, result=-1, data=Intent { (has extras) }} to activity {com.machemarket.android/com.machemarket.android.ui.activity.MainActivity}: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.String
at android.app.ActivityThread.deliverResults(ActivityThread.java:5230)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5271)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.lang.String
at com.machemarket.android.ui.fragment.PublishFragment.onActivityResult(PublishFragment.kt:147)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at android.app.Activity.dispatchActivityResult(Activity.java:8292)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5223)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5271)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
brookmg commented
Nvm...
RageshAntony commented
Nvm...
how to resolve this ? @brookmg
brookmg commented
I'm really sorry. I should've explained how I solved this.
val paths = data?.getParcelableArrayListExtra<Uri>(KEY_SELECTED_MEDIA)I was casting the ArrayList elements into String on the question. But they were actually Uri objects. @RageshAntony