Created
February 26, 2020 13:55
-
-
Save sushant-j/ce512284a832aebc7bb805e34087dc55 to your computer and use it in GitHub Desktop.
Cloudinary Media Upload snippet - Java, Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Uploads media to Cloudinary. | |
*/ | |
private void uploadToCloudinary() { | |
try { | |
if (!NetworkUtils.isInternetAvailable(UploadService.this)) { | |
setPostState(PostEntity.ERROR_MEDIA); | |
notifyFailureAndNext(); | |
return; | |
} | |
String filePath = null; | |
String uploadPreset = null; | |
Map<String, Object> options = new HashMap<>(); | |
switch (mMediaEntity.getType()) { | |
case MediaEntity.PHOTO: | |
filePath = mMediaEntity.getDirPath() + "/" + Constants.MEDIA_FILE_PROCESSED_JPG; | |
uploadPreset = FirebaseRemoteConfig.getInstance(). | |
getString(RemoteConfigUtil.CLOUDINARY_RATING_IMAGE_PRESET); | |
break; | |
case MediaEntity.VIDEO: | |
filePath = mMediaEntity.getDirPath() + "/" + Constants.MEDIA_FILE_PROCESSED_MP4; | |
uploadPreset = FirebaseRemoteConfig.getInstance(). | |
getString(RemoteConfigUtil.CLOUDINARY_RATING_VIDEO_PRESET); | |
options.put("resource_type", "video"); | |
options.put("video_codec", "vc_auto"); | |
break; | |
default: | |
//No action required | |
break; | |
} | |
UploadPolicy policy = new UploadPolicy.Builder() | |
.maxRetries(3) | |
.backoffCriteria(10000, UploadPolicy.BackoffPolicy.LINEAR) | |
.build(); | |
String requestId = MediaManager.get() | |
.upload(filePath) | |
.options(options) | |
.unsigned(uploadPreset) | |
.policy(policy) | |
.callback(UploadService.this) | |
.dispatch(UploadService.this); // or startNow() | |
LogUtils.i("Upload request dispatched to Cloudinary."); | |
} catch (Exception e) { | |
LogUtils.e("Error creating Cloudinary upload request", e); | |
setMediaState(MediaEntity.ERROR_UPLOAD); | |
setPostState(PostEntity.ERROR_MEDIA); | |
notifyFailureAndNext(); | |
} | |
} | |
/** | |
* Cloudinary callback called when a request starts uploading. | |
*/ | |
@Override | |
public void onStart(String requestId) { | |
LogUtils.i("Cloudinary upload started for request " + requestId); | |
mUploadProgress = 0; | |
} | |
/** | |
* Cloudinary callback called when a request starts uploading. The progress is converted to a | |
* value in the range [51 - 100] for video and [1 - 100] for photo. | |
*/ | |
@Override | |
public void onProgress(String requestId, long bytes, long totalBytes) { | |
int newProgress = (int) Math.round(((double) bytes / totalBytes) * 100); | |
if (newProgress - mUploadProgress > 5) { // publish updates at every 5% | |
mUploadProgress = newProgress; | |
mPostEntity.setProgress(newProgress); | |
LogUtils.d("Cloudinary progress update - newProgress: " + newProgress); | |
UploadBroadcaster.sendBroadcastUploadProgress(this, mPostEntity, mMediaEntity, | |
mCurrentJobIndex + 1, mJobs.size()); | |
} | |
} | |
/** | |
* Cloudinary callback called when a request succeeds. | |
*/ | |
@Override | |
public void onSuccess(String requestId, Map resultData) { | |
LogUtils.i("Cloudinary upload success for request " + requestId); | |
try { | |
// save public id | |
String[] parts = String.valueOf(resultData.get("public_id")).split("/"); | |
String publicId = parts[1]; | |
String uploadType = String.valueOf(resultData.get("resource_type")); | |
setMediaPublicId(publicId); | |
setMediaState(MediaEntity.DONE); | |
addPostToServer(); | |
} catch (Exception e) { | |
LogUtils.e("Error reading public id", e); | |
setMediaState(MediaEntity.ERROR_UPLOAD); | |
setPostState(PostEntity.ERROR_MEDIA); | |
notifyFailureAndNext(); | |
} | |
} | |
/** | |
* Cloudinary callback called when a request fails. | |
*/ | |
@Override | |
public void onError(String requestId, ErrorInfo error) { | |
LogUtils.i("Cloudinary upload error for request " + requestId + ". Error: " + | |
error.getCode() + " " + error.getDescription()); | |
if (error.getCode() == ErrorInfo.REQUEST_CANCELLED) { | |
LogUtils.i("Cloudinary request cancelled"); | |
return; | |
} | |
sendEventCloudinaryUploadFailure(error); | |
setMediaState(MediaEntity.ERROR_UPLOAD); | |
setPostState(PostEntity.ERROR_MEDIA); | |
notifyFailureAndNext(); | |
} | |
/** | |
* Cloudinary callback called when a request is rescheduled. | |
*/ | |
@Override | |
public void onReschedule(String requestId, ErrorInfo error) { | |
LogUtils.i("Cloudinary upload rescheduled for request " + requestId + ". Error: " + | |
error.getCode() + " " + error.getDescription()); | |
setMediaState(MediaEntity.ERROR_UPLOAD); | |
setPostState(PostEntity.ERROR_MEDIA); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment