'ACTION_MEDIA_SCANNER_SCAN_FILE:String' is deprecated

左心房为你撑大大i 提交于 2021-02-19 00:44:26

问题


activity?.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(copyFile)))

I got the warning "ACTION_MEDIA_SCANNER_SCAN_FILE is deprecated." in above code

Is there a simple replacement code?

Thank you


回答1:


ACTION_MEDIA_SCANNER_SCAN_FILE Deprecated!

You can use the following

Java

File file = new File(filePath);
MediaScannerConnection.scanFile(context,
                    new String[]{file.toString()},
                    null, null);

Kotlin

val file = File(filePath) 
MediaScannerConnection.scanFile(context, arrayOf(file.toString()),
      null, null)

This will request the media scanner to scan the files at the specified path. Also, it is worth noticing that 3rd and 4th param in scanFile() method is null in the above example. However the third param can be used to optionally specify the MIME TYPE of the file and the fourth param can be used to supply a listener to listen to the scan completion event.

Refer developer site for details

Following is the deprecated way

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));



回答2:


From https://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_SCANNER_SCAN_FILE

This constant was deprecated in API level 29. Callers should migrate to inserting items directly into MediaStore, where they will be automatically scanned after each mutation



来源:https://stackoverflow.com/questions/60203353/action-media-scanner-scan-filestring-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!