using FFMPEG with MediaStore Uri in Android Q

Viewed 212

I'm trying to convert MP4 which placed in MediaStore to GIF. with this library

But, it does not seem to work with MediaStore Uri

This is my code :

fun convertToGif(fileUri: Uri): Boolean {
    var result = false
    val newFileName = fileUri.toString().substringAfterLast("/").substringBefore(".") + ".gif"

    val values = ContentValues()
    values.put(MediaStore.Images.Media.DISPLAY_NAME, newFileName)
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/gif")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        values.put(MediaStore.Images.Media.IS_PENDING, 1)
    }
    val contentResolver: ContentResolver = context.contentResolver
    val newFileUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)!!
    try {
        val pdf = contentResolver.openFileDescriptor(newFileUri, "w", null)
        if (pdf == null) {
            throw NullPointerException("ParcelFileDescriptor is Null")
        } else {
            val source = File(fileUri.toString())
            val destination = File(newFileUri.toString())
            val command = "-i $source $destination"
            result = when (FFmpeg.execute(command)) {
                Config.RETURN_CODE_SUCCESS -> {
                    Log.i(Config.TAG, "Command execution completed successfully.")
                    true
                }
                Config.RETURN_CODE_CANCEL -> {
                    Log.i(Config.TAG, "Command execution cancelled by user.")
                    false
                }
                else -> {
                    Log.i(
                        Config.TAG,
                        String.format("Command execution failed with rc=%d and the output below.", result)
                    )
                    Config.printLastCommandOutput(Log.INFO)
                    false
                }
            }
        }
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return result
}

Is there any way I can solve this problem?

0 Answers
Related