Mher Arsh
Mher Arsh

Reputation: 597

UploadTask (Google Play services) continue uploading on process restart

I use UploadTask to upload files to Firebase Storage, how can I continue uploading a file after restarting the process? every time I try to upload a file that is not fully uploaded, the upload starts from the beginning.

 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)
        }
   }

Upvotes: 0

Views: 122

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317760

It's not possible to continue an upload if the process was terminated in the middle of a prior upload. The Cloud Storage SDK simply does not do that. Instead, you should use some mechanism to make it more likely that the process stays alive as long as needed to complete the upload, such as a foreground service.

Upvotes: 1

Related Questions