我使用UploadTask将文件上传到Firebase存储,如何在重新启动进程后继续上传文件?每次我尝试上传未完全上传的文件时,都会从头开始上传。
private fun uploadFromUri(fileUri: Uri) {
val fileFromUri = Uri.fromFile(File(Utils.getPath(this, fileUri)))
val photoRef = storageRef.child("files")
.child(fileFromUri.lastPathSegment ?: "_file_")
val metadata = StorageMetadata.Builder()
.setCustomMetadata("fileName", fileFromUri.lastPathSegment)
.build()
photoRef.putFile(fileUri, metadata).addOnProgressListener { taskSnapshot ->
showProgressNotification(
getString(R.string.progress_uploading),
taskSnapshot.bytesTransferred,
taskSnapshot.totalByteCount
)
}.continueWithTask { task ->
if (!task.isSuccessful) {
throw task.exception!!
}
photoRef.downloadUrl
}.addOnSuccessListener { downloadUri ->
broadcastUploadFinished(downloadUri, fileUri)
showUploadFinishedNotification(downloadUri, fileUri)
}.addOnFailureListener { exception ->
broadcastUploadFinished(null, fileUri)
showUploadFinishedNotification(null, fileUri)
}
}
如果在上次上传过程中终止进程,则无法继续上传。云存储SDK根本不这样做。相反,您应该使用某种机制来使进程保持活动的可能性更大,只要完成上载所需,例如前台服务。