Last active
September 9, 2019 10:51
-
-
Save IslamKhSh/2e2cb00e823be2dfc4223ce04b5edafd to your computer and use it in GitHub Desktop.
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
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