androidkotlinandroid-contentproviderandroid-10.0android-mediascanner

Android Q saving media files not showing in Gallary


I'm using Samsung a51. API level 10

I'm saving different type of file in Download folder like - image, video, song, zip

code

val filename = "exampleFileName.jpg"

            val now = System.currentTimeMillis() / 1000
            val values = ContentValues().apply {
                put(MediaStore.Downloads.DATE_ADDED, now)
                put(MediaStore.Downloads.DATE_MODIFIED, now)
                put(MediaStore.Downloads.DISPLAY_NAME, filename)
                put(MediaStore.Downloads.TITLE, filename.stripExtension())
                put(MediaStore.Downloads.SIZE, contentSize)
                put(MediaStore.Downloads.RELATIVE_PATH, "Download/my-appname")
                put(MediaStore.Downloads.IS_PENDING, 1)
            }

val uri: Uri? = null

waitForExecution(handler) {
   uri = resolver.insert(
    MediaStore.Downloads.getContentUri(volume),
    values
    )
}

val descriptor = resolver.openFileDescriptor(uri!!, "w")
if (descriptor != null) {
  val outputStream = FileOutputStream(descriptor.fileDescriptor)
  val written = writeFile(outputStream)
  if(written){
     values.clear()
     values.put(MediaStore.Downloads.IS_PENDING, 0)
     if(mimeType != null) values.put(MediaStore.Downloads.CONTENT_TYPE, mimeType)
     waitForExecution(handler) { // ui thread
      resolver.update(uri!!, values, null, null)
      // android says that content resolver automatically notify to system don't need to send brodcast.
      val intent = Intent(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE))
      context.sendBroadcast(intent)
      }
  }else{
     dontThrow { resolver.delete(uri!!, null, null) }
  }
} else {
   v("got null descriptor.")
}

Everything is fine file written successfully. file showing in File Manager

but the problem is not showing in the gallery. and in some other mobile also not working

it also shows in Mx player

is there any to notify that file is added

or android 10 bug?


Solution

  • Problem resolved by setting mimeType when inserting