问题描述
我有使用API级别29下的ContentResolver openFile方法的源代码:
val file = contentResolver.openFile(uri,"r",null)
对于我的特定项目,我需要使用API级别28。
设置API级别28后同步Gradle时,出现以下错误: “未解决的参考:openFile”
通过检查符号,我尝试了以下更改:
val providerClient = contentResolver.acquireContentProviderClient(uri)
val file = providerClient?.openFile(uri,"r")
然后我收到警告,说应该释放提供程序客户端。
private fun performInstallation(uri: Uri) {
try {
val packageInstaller = packageManager.packageInstaller
val sessionId = packageInstaller.createSession(
PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL
)
)
val session = packageInstaller.openSession(sessionId)
val uniqueName = UUID.randomUUID().toString()
//
val file = contentResolver.openFile(uri,null)
val size = file?.statSize ?: throw IOException("Couldn't read file from provider!")
file.close()
val outputStream = session.openWrite(uniqueName,size)
val inputStream = contentResolver.openInputStream(uri)
?: throw IOException("Couldn't read file from provider!")
val buffer = ByteArray(1024)
outputStream.use { output ->
inputStream.use { input ->
do {
val read = input.read(buffer)
if (read > 0) {
output.write(buffer,read)
}
} while (read != -1)
}
session.fsync(output)
}
val intent = Intent(ACTION_RESULT).also { it.putExtra(EXTRA_RESULT,"success") }
val broadcast = PendingIntent.getbroadcast(
applicationContext,intent,PendingIntent.FLAG_ONE_SHOT
)
session.commit(broadcast.intentSender)
log("Successfully installed $uri!")
} catch (e: Exception) {
val intent = Intent(ACTION_RESULT).also { it.putExtra(EXTRA_RESULT,e.message) }
sendbroadcast(intent)
loge("Error installing $uri",e)
}
mainHandler.post {
stopForeground(true)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)