Last active
July 20, 2021 14:06
-
-
Save inktomi/5391583 to your computer and use it in GitHub Desktop.
Multipart Upload
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
// 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); | |
} | |
}); |
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
@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); |
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
This set up results in having multipart params, but no multipart files in the HttpServletRequest on the API side.