BeaconNetwork.downloadDFU(parameter).apply {
if (Utils.isExternalStorageLegacy()) {
// 分区存储
val contentUri = MediaStore.Downloads.getContentUri("external")
val contentValues = ContentValues()
contentValues.put(
MediaStore.Downloads.RELATIVE_PATH,
Environment.DIRECTORY_DOWNLOADS
)
contentValues.put(
MediaStore.Downloads.DISPLAY_NAME,
"${fileName}.dfu"
)
contentResolver.insert(contentUri, contentValues)?.let {
launch(Dispatchers.IO){
val readBytes = byteStream().readBytes()
contentResolver.openOutputStream(it)?.use {out ->
out.write(readBytes)
}
fileNameLiveData.postValue("${fileName}")
fileByteLiveData.postValue(readBytes)
fileSizeLiveData.postValue(readBytes.size)
handler.sendEmptyMessage(0x0002)
}
}?: let {
Log.e(TAG, "downloadDfu: null" )
handler.sendEmptyMessage(0x0002)
}
return@coroutineScope
}
}
同一份代码,有些文件无法保存。控制台打印
E/SQLiteDatabase: Error inserting UNIQUE constraint failed: files._data (Sqlite code 2067 SQLITE_CONSTRAINT_UNIQUE), (OS error - 2:No such file or directory)
downloadDfu: null
但是是修改文件名之后就能正常保存。不知道是什么原因,有大神知道吗
Error inserting UNIQUE constraint failed
插入错误,首先是约束问题,要求唯一。“但是是修改文件名之后就能正常保存”
对吧。