UploadTask(Google Play服务)会继续上传流程重启

问题描述 投票:0回答:1

我使用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)
        }
   }
android firebase google-cloud-storage firebase-storage
1个回答
1
投票

如果在上次上传过程中终止进程,则无法继续上传。云存储SDK根本不这样做。相反,您应该使用某种机制来使进程保持活动的可能性更大,只要完成上载所需,例如前台服务。

© www.soinside.com 2019 - 2024. All rights reserved.