Created
May 15, 2019 15:36
-
-
Save esabook/fd2ba84061cc19e428e6243744cb9926 to your computer and use it in GitHub Desktop.
send Android local image as web-form for retrofit
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
void sendImage(String uriString) { | |
Uri selectedImage = Uri.parse(uriString); | |
String[] filePathColumn = {MediaStore.Images.Media.DATA}; | |
// Get the cursor | |
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); | |
if (cursor == null) return; | |
// Move to first row | |
cursor.moveToFirst(); | |
//Get the column index of MediaStore.Images.Media.DATA | |
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | |
//Gets the String value in the column | |
String imgDecodableString = cursor.getString(columnIndex); | |
cursor.close(); | |
sendImage(new File(imgDecodableString), uriString); | |
} | |
void sendImage(File file, String originalUriString) { | |
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath()); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
bmp.compress(Bitmap.CompressFormat.WEBP, 80, bos); | |
AlertDialog b = new AlertDialog.Builder(getActivity()).setMessage(String.format(getString(R.string.upload_image_with_compressed_size_kb), bos.size() / 1024)) | |
.setPositiveButton("Upload", (dialog, which) -> { | |
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); | |
RequestBody body = RequestBody.create(MultipartBody.FORM, bos.toByteArray()); | |
builder.addFormDataPart("image", file.getName(), body); | |
Call<__ReportImageDTO> apiCall = apiService.postImage(builder.build(), originalUriString); | |
apiCalls.add(apiCall); | |
}) | |
.setNegativeButton("Later", (dialog, which) -> { | |
dialog.dismiss(); | |
}) | |
.create(); | |
b.setOnDismissListener(dialog -> { | |
queuingUploadOrNotDialog.remove(dialog); | |
if (queuingUploadOrNotDialog.isEmpty()) startUploadingImages(); | |
}); | |
queuingUploadOrNotDialog.add(b); | |
b.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment