I am trying to make a function that deletes a selected video from a recycler view. After the confirmation in the confirmDeleteDialog I receive “Failed to delete video” , knowing that the same code was working fine in the same device before adding the “READ_MEDIA_VIDEO” and the “READ_MEDIA_VISUAL_USER_SELECTED” permissions in the manifest .
Below is the delete function, the function used to get the videos and the manifest file :
binding.deleteVideoButton.setOnClickListener {
//check this section there is something wrong with it
val file = File(currentVideoUri.path.toString())
if (file.exists() && !lock) {
Utilities.moveViewUpAndBack(binding.deleteVideoButton)
val confirmDeleteDialog = AlertDialog.Builder(this)
.setTitle(getString(R.string.delete_video))
.setMessage(getString(R.string.deletion_confirmation))
.setPositiveButton(getString(R.string.yes)) { _, _ ->
binding.progressBar.visibility = View.VISIBLE
CoroutineScope(Dispatchers.IO).launch {
val deleted = file.delete()
withContext(Dispatchers.Main) {
binding.progressBar.visibility = View.GONE
if (deleted) {
val tempoVideos = Utilities.listVideos(applicationContext)
val tempoThumbnails = Utilities.convertFilesToBitmaps(tempoVideos.take(5))
val tempoUris = tempoVideos.map { Uri.fromFile(it) }
if (tempoUris.isNotEmpty()) {
currentVideoUri = tempoUris[0]
preparePlayer(currentVideoUri)
}
adapter.updateThumbnails(tempoThumbnails, tempoUris)
loadRemainingBitmaps(tempoVideos.drop(5))
} else {
Toast.makeText(this@PreviewActivity,
getString(R.string.failed_to_delete_video),
Toast.LENGTH_SHORT).show()
}
//alertDialog.dismiss()
}
}
}
.setNegativeButton(getString(R.string.no), null)
.create()
confirmDeleteDialog.show()
} else {
Toast.makeText(this, getString(R.string.file_not_found), Toast.LENGTH_SHORT).show()
}
}
listVideos() function
fun listVideos(context: Context): List<File> {
val videos = mutableListOf<File>()
val projection = arrayOf(MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DATE_ADDED,
MediaStore.Video.Media.DATA)
val selection =
"${MediaStore.Video.Media.DISPLAY_NAME} LIKE ? AND ${MediaStore.Video.Media.DISPLAY_NAME} NOT LIKE ? AND ${MediaStore.Video.Media.DISPLAY_NAME} LIKE ?"
val selectionArgs = arrayOf("%camerago_%", "%.pending%", "%.mp4")
val sortOrder = "${MediaStore.Video.Media.DATE_ADDED} DESC"
val query = context.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder)
query?.use { cursor ->
val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)
val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
while (cursor.moveToNext()) {
val data = cursor.getString(dataColumn)
//Log.e("logo", "video name: $name")
videos.add(File(data))
}
}
return videos
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<uses-feature
android:name="android.hardware.camera2"
android:required="false" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<!--implementation of a file provider in order to be able to share the video to other platforms-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.nerovero.camerago.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!--check whether enabled attribute is necessary in the service or not -->
<service
android:name=".VideoRecordingService"
android:foregroundServiceType="camera|microphone"
android:enabled="true"
android:exported="false"
/>
<activity
android:name=".SettingsActivity"
android:enabled="true"
android:exported="false" />
<activity
android:name=".PreviewActivity"
android:exported="true" >
</activity>
<activity
android:name=".CameraActivity"
android:exported="true"
android:showWhenLocked="true"
android:turnScreenOn="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I tried the contentResolver method but it does not work as well.