Using upload SDK for Android
Using upload SDK for Android
Upload a video file in chunks, handle progress, and manage network events. The code uses the FastPixUploadSdk to configure and initiate the upload process, implementing callbacks to monitor the upload status.
1. Set up the activity and implement callbacks
The UploadActivity class extends AppCompatActivity and implements the FastPixUploadCallbacks interface to handle upload events. This sets up the foundation for initializing the SDK and receiving updates on the upload process.
2. Initialize the file and upload parameters
3. Configure the FastPix upload SDK
4. Start the upload process
5. Monitor upload progress
6. Handle upload success or failure
1class UploadActivity : AppCompatActivity(), FastPixUploadCallbacks {
2
3 private lateinit var sdk: FastPixUploadSdk
4
5 override fun onCreate(savedInstanceState: Bundle?) {
6 super.onCreate(savedInstanceState)
7
8 val file = File(filesDir, "your-video.mp4")
9 val signedUrl = "https://your-signed-upload-url" // Replace with actual signed URL
10 val chunkSize = 5 // Chunk size in MB
11
12 sdk = FastPixUploadSdk.Builder(this)
13 .setFile(file)
14 .setSignedUrl(signedUrl)
15 .setChunkSize(chunkSize * 1024 * 1024) // Convert MB to bytes
16 .setMaxRetries(3) // Optional: Number of retries for failed chunks
17 .setRetryDelay(2000) // Optional: Delay between retries in milliseconds
18 .callback(this) // Set this activity as the callback handler
19 .build()
20
21 sdk.startUpload() // Start the upload
22 }
23
24 override fun onProgressUpdate(progress: Double) {
25 Log.d("Upload", "Progress: $progress%")
26 }
27
28 override fun onSuccess(timiMillis: Long) {
29 Log.d("Upload", "Upload completed in $timiMillis ms")
30 }
31
32 override fun onError(error: String, timiMillis: Long) {
33 Log.e("Upload", "Error: $error after $timiMillis ms")
34 }
35
36 override fun onNetworkStateChange(isOnline: Boolean) {
37 Log.d("Upload", if (isOnline) "Back online" else "Offline")
38 }
39
40 override fun onUploadInit() {
41 Log.d("Upload", "Upload initialized")
42 }
43
44 override fun onAbort() {
45 Log.d("Upload", "Upload aborted by user")
46 }
47
48 override fun onChunkHandled(
49 totalChunks: Int,
50 filSizeInBytes: Long,
51 currentChunk: Int,
52 currentChunkSizeInBytes: Long
53 ) {
54 Log.d("Upload", "Uploaded chunk $currentChunk/$totalChunks")
55 }
56
57 override fun onChunkUploadingFailed(
58 failedChunkRetries: Int,
59 chunkCount: Int,
60 chunkSize: Long
61 ) {
62 Log.e("Upload", "Chunk $chunkCount failed after $failedChunkRetries retries")
63 }
64
65 override fun onPauseUploading() {
66 Log.d("Upload", "Upload paused")
67 }
68
69 override fun onResumeUploading() {
70 Log.d("Upload", "Upload resumed")
71 }
72}