Advertisement
BlackZerg

Untitled

May 11th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. public String uploadPhoto(String image) {
  2.         PhotoUploadProcessor photoUploadProcessor = getPhotoUploadProcessor();
  3.         photoUploadProcessor.upload(image, photoId -> {
  4.             Log.d("0. MonitoringPresenter", String.format("photo id => %s", photoId));
  5.         });
  6.         return photoId;
  7.     }
  8.  
  9.  
  10. public class PhotoUploadProcessor {
  11.  
  12.     public interface OnUploadCallback {
  13.         void onUploaded(String photoId);
  14.     }
  15.  
  16.     private ImageService imageService;
  17.  
  18.     public PhotoUploadProcessor(ImageService imageService) {
  19.         this.imageService = imageService;
  20.     }
  21.  
  22.     public void upload(String image, final PhotoUploadProcessor.OnUploadCallback callback) {
  23.         File file = new File(image);
  24.         RequestBody requestFile = RequestBody.create(MediaType.parse(image), file);
  25.         MultipartBody.Part multipart = MultipartBody.Part
  26.                 .createFormData("photo", file.getName(), requestFile);
  27.  
  28.         imageService.uploadPhoto(multipart, new Callback<UploadPhoto>() {
  29.             @Override
  30.             public void onSuccess(UploadPhoto result) {
  31.                 callback.onUploaded(result.getPhoto().getId().toString());
  32.             }
  33.  
  34.             @Override
  35.             public void onFailure(@NonNull Call<ApiResponse<UploadPhoto>> call, @NonNull Throwable t) {
  36.                 Log.d("PhotoUploadProcessor", String.format("onFailure call => %s, Throwable => %s", call.request(), t.getMessage()));
  37.                 super.onFailure(call, t);
  38.             }
  39.  
  40.             @Override
  41.             public void onFailure(int statusCode, List<ApiError> errors) {
  42.                 Log.d("PhotoUploadProcessor", String.format("statusCode call => %s, errors => %s", statusCode, errors));
  43.                 super.onFailure(statusCode, errors);
  44.             }
  45.         });
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement