0

I am a beginner in Kotlin, and just started Kotlin. I am trying to get thumbnail from a video that i recorded within my app.

I have tried almost everything that i possibly can, but everytime i get NULL.

So first of all, i added the permission of READ_EXTERNAL_STORAGE in the manifest (i don't know, maybe it's required or not)

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

i have a video file that i created using:

fun createVideoFile(): File {

        val fileName = "GameXVideo"
        val storageDir = getExternalFilesDir(Environment.DIRECTORY_MOVIES)

        return File.createTempFile(fileName, "mp4", storageDir)

    }

so i am not saving the video in the physical storage. right after finishing recording, i am playing back this recorded video, and it is working pretty fine.

The next thing i want to get is it's thumbnail, i have tried lots of already given answers but i cannot get it done. here is what i have already tried:

val videoFile = intent.extras?.get("videoFile") as? File // getting videoFile from the intent
val videoPath = videoFile?.absolutePath // getting its absolute path
val videoUri = Uri.fromFile(videoFile) // getting its uri

Method 1:

val bMap = ThumbnailUtils.createVideoThumbnail(videoPath!!, MediaStore.Video.Thumbnails.MICRO_KIND) // gives null

Method 2:

val mMMR = MediaMetadataRetriever()
mMMR.setDataSource(this, videoUri)
val bmp = mMMR.frameAtTime // also gives null

Method 3:

val asd: InputStream? = contentResolver.openInputStream(videoUri)
val bmap2 = BitmapFactory.decodeStream(asd)
asd?.close()

Method 4:

val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = this.contentResolver.query(videoUri, filePathColumn, null, null, null)
cursor?.moveToFirst()
val columnIndex = cursor?.getColumnIndex(filePathColumn[0])
val picturePath = cursor?.getString(columnIndex!!)
cursor?.close()

your HELP will be highly appreciated. Thank you.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 1
    Why don't you use the Glide library? Here's is the guide on how to use https://futurestud.io/tutorials/glide-thumbnails – Rajnish suryavanshi Feb 13 '20 at 08:32
  • try https://stackoverflow.com/a/52312638/8325853 – Tushar Lathiya Feb 13 '20 at 08:33
  • 1
    @Rajnishsuryavanshi yes it worked with Glide... Thank you very much. I wanted to do this thumbnail work without using any third party library, but i am not seeing any other way rather than this Glide. So i will stick with it for now. :-) – hamza-faroooq Feb 13 '20 at 09:57

1 Answers1

1

MediaStore.Videos.Thumbnails is deprecated in API 29 https://developer.android.com/reference/android/provider/MediaStore.Video.Thumbnails and they recommend to use ContentProvider.loadThumbnail https://developer.android.com/reference/android/content/ContentResolver.html#loadThumbnail(android.net.Uri,%20android.util.Size,%20android.os.CancellationSignal)

Maybe give it a try and let us know if it worked?

  • i think (loadThumbnail) was used in older android APIs. When using this i got: Call requires API level Q(Current min is 21). I don't want to lower the API level anymore. So i guess this answer is not for me... :-/ – hamza-faroooq Feb 13 '20 at 10:02