A Multipart upload solution on Android, including progress notification and network change. This is a project based on AOSP DownloadManager and I made this with kotlin and OkHttp3.
- get a instance of UploadManager
val uploadManager: UploadManager = UploadManager.getUploadManger(context)
// init it when your app boots
uploadManager.init()
The best practise is to call UploadManager.init()
as early as you need to display
notifications related to upload tasks and to clear/resume the crashed tasks
- upload
val uri = Uri.parse(info.fileUri)
val fileHeaders = mutableMapOf(Pair("User-Agent", "Box Android")).toHeaders()
// dsl for building request
val request = request {
targetUrl = UPLOAD_URI.toString().toHttpUrl()
parts{
+Part("filecount", "1")
+Part("predefine", "1")
+Part("token", info.code)
+Part("secret", info.password)
+Part("expiration", Expiration.toSeconds(info.expireTimeOption!!).toString())
+Part("file", fileInfo = FileInfo(uri, (info.mimeType
?: "application/octet-stream").toMediaTypeOrNull(), info.fileName))
}
headers= fileHeaders
}
uploadManage.enqueue(request.build())
- cancel a upload task
uploadManger.cancel(uploadId)
- restart a upload task
uploadManger.restartUpload(uploadId)
- query a upload task
TODO
This project already add consumerProguardFiles
, so feel free to use proguard.
For now, you have to download source and install it to local maven. I will publish it to online repo soon.
-
clone this repo and run
gradlew lib:install
this will install the UploadManager library to your local maven repo (in~/.m2
) -
edit
yourProject/app/build.gradle
add
repositories { google() jcenter() mavenLocal() }
and
dependencies { implementation 'me.ctknight.uploadmanager:lib:$latest-version' }
be sure to replace the $latest-version with correct version number
Copyright 2018 Lai Jiewen.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
see also: LICENSE file
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.