I'm trying to open the gallery and be able to select a video to delete in my app. I used this code:
private val pickVideoLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
videoUrl = data?.data
// Handle the selected video URI
}
}
private val deleteVideoLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
videoUrl?.let { videoUri ->
// The video has been deleted, perform necessary actions
// after deleting the selected video
}
}
}
binding.deleteVideo.setOnClickListener {
deleteOriginalVideoFromGallery(videoUrl)
}
private fun deleteOriginalVideoFromGallery(videoUri: Uri?) {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
intent.type = "video/*"
pickVideoLauncher.launch(intent)
videoUri?.let { videoUri ->
val deleteIntent = Intent(Intent.ACTION_DELETE)
deleteIntent.data = videoUri
deleteVideoLauncher.launch(deleteIntent)
}
}
However, I get this error:
E/AndroidRuntime: FATAL EXCEPTION: main Process: harmony.valley.wamboocam, PID: 23073 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DELETE dat=content://media/... } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2174) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1805) at android.app.Activity.startActivityForResult(Activity.java:5596) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728) at androidx.core.app.ActivityCompat$Api16Impl.startActivityForResult(ActivityCompat.java:809) at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:246) at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:243) at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:175) at androidx.fragment.app.Fragment$10.launch(Fragment.java:3622) at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:47) at harmony.valley.wamboocam.HomeFragment.deleteOriginalVideoFromGallery(HomeFragment.kt:1043) at harmony.valley.wamboocam.HomeFragment.initUI$lambda$24$lambda$23(HomeFragment.kt:763)
I tried deleting this line:
deleteVideoLauncher.launch(deleteIntent)
but then, the Gallery opens and I'm not able to select the video to delete it. When I tap on it, the Gallery closes and I go back to the app as if nothing would have happened.
How can I solve this? At least, I would like to be able to select the video from the Gallery and delete it.