项目场景:
通过kotlin可以比较方便的实现文件的操作,示例是复制图片、视频文件到图库,图库立即更新
示例实现:
var appDir = File(Environment.getExternalStorageDirectory(), "test")
//新建目录
if (!appDir.exists()) {
appDir.mkdir()
}
var file: File
launch(Dispatchers.IO) {
var format = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US)
//这里可自行加上图片或视频的后缀名
var fileName = format.format(Date())
file = File(appDir, fileName)
//复制文件到目标文件
File(test.path).copyTo(file)
}
//发送广播
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.data = Uri.fromFile(file)
this.sendBroadcast(intent)
后记:
这只是本人对kotlin文件操作的小小理解,如有不对,还望指教