Skip to content

Instantly share code, notes, and snippets.

@inktomi
Last active July 20, 2021 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inktomi/5391583 to your computer and use it in GitHub Desktop.
Save inktomi/5391583 to your computer and use it in GitHub Desktop.
Multipart Upload
// From Camera
Bitmap photo = (Bitmap) data.getExtras().get("data");
File imageFileFolder = new File(getCacheDir(),"Avatar");
if( !imageFileFolder.exists() ){
imageFileFolder.mkdir();
}
FileOutputStream out = null;
File imageFileName = new File(imageFileFolder, "avatar-" + System.currentTimeMillis() + ".jpg");
try {
out = new FileOutputStream(imageFileName);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
} catch (IOException e) {
Log.e(TAG, "Failed to convert image to JPEG", e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
Log.e(TAG, "Failed to close output stream", e);
}
}
TypedFile image = new TypedFile("image/jpeg", imageFileName);
mUserService.uploadAvatar(mAuthToken, image, new Callback<Boolean>() {
@Override
public void success(Boolean aBoolean, Response response) {
Log.v(TAG, "Avatar successfully saved to server");
}
@Override
public void failure(RetrofitError error) {
Log.e(TAG, "Failed to save user avatar: " + error.getResponse().getReason(), error);
}
});
@Multipart
@POST("/user/me/avatar?access_token={access_token}")
void uploadAvatar(@Name("access_token") String accessToken, @Name("avatar") TypedFile image, retrofit.http.Callback<Boolean> callback);
@inktomi
Copy link
Author

inktomi commented Apr 15, 2013

This set up results in having multipart params, but no multipart files in the HttpServletRequest on the API side.

Copy link

ghost commented Jul 20, 2021

Why do I have to save it before uploading? What if I already have the bitmap and I just want to convert that to a bytearray and upload it without saving the image locally to my device? This feature of uploading an image without saving it to the phone should be in your library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment