Skip to content

Instantly share code, notes, and snippets.

@IslamKhSh
Last active September 9, 2019 10:51
Show Gist options
  • Save IslamKhSh/2e2cb00e823be2dfc4223ce04b5edafd to your computer and use it in GitHub Desktop.
Save IslamKhSh/2e2cb00e823be2dfc4223ce04b5edafd to your computer and use it in GitHub Desktop.
Uri selectedImage = data.getData(); // or your uri
String selectedMediaPath;
Cursor cursor = getActivity().getContentResolver().query(selectedImage, null,null, null, null);
if (cursor == null)
selectedMediaPath = selectedImage.getPath;
else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
selectedMediaPath = cursor.getString(idx);
}
cursor.close();
File userPhotoFile = new File(selectedMediaPath);
RequestBody userFileRequest = RequestBody.create(MediaType.parse(guessContentTypeFromName(userPhotoFile.name)), userPhotoFile);
MultipartBody.Part mediaPart; // send it with retrofit as parameter with @Part
try {
mediaPart = MultipartBody.Part.createFormData("image", userPhotoFile.getName(), userFileRequest);
} catch (e: IllegalArgumentException) {
mediaPart = MultipartBody.Part.createFormData("image", "not_supported_file_name", userFileRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment