Advertisement
GerONSo

Untitled

Jun 4th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package com.example.maxim.picoto.helpers;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.AsyncTask;
  6.  
  7. import android.util.Base64;
  8. import android.util.Log;
  9.  
  10. import com.example.maxim.picoto.data.AsyncSendImageData;
  11. import com.example.maxim.picoto.interfaces.ServerService;
  12. import com.example.maxim.picoto.utils.FileUtils;
  13. import com.google.gson.Gson;
  14. import com.google.gson.GsonBuilder;
  15.  
  16. //import org.apache.commons.codec.binary.Base64;
  17.  
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21.  
  22. import okhttp3.MediaType;
  23. import okhttp3.MultipartBody;
  24. import okhttp3.RequestBody;
  25. import retrofit2.Call;
  26. import retrofit2.Callback;
  27. import retrofit2.Response;
  28. import retrofit2.Retrofit;
  29. import retrofit2.converter.gson.GsonConverterFactory;
  30. import static com.example.maxim.picoto.MainActivity.FILES_DIR;
  31.  
  32. public class HttpServerHelper {
  33.  
  34. private static Bitmap resultImage;
  35.  
  36. public static Bitmap sendImage(File file, int styleType) {
  37. Gson gson = new GsonBuilder()
  38. .setLenient()
  39. .create();
  40. Retrofit retrofit = new Retrofit.Builder()
  41. .baseUrl("http://194.87.103.212:4567/")
  42. .addConverterFactory(GsonConverterFactory.create())
  43. .build();
  44. ServerService api = retrofit.create(ServerService.class);
  45. MultipartBody.Part filePart = MultipartBody.Part.
  46. createFormData("file", file.getName(),
  47. RequestBody.create(MediaType.parse("image/*"), file));
  48. Call<String> call = api.upload(filePart);
  49.  
  50. call.enqueue(new Callback<String>() {
  51. @Override
  52. public void onResponse(Call<String> call, Response<String> response) {
  53. if (response.body() != null) {
  54. String s = response.body();
  55. byte[] bytes = Base64.decode(s, Base64.DEFAULT);
  56. resultImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  57. System.out.println(response.body());
  58. }
  59. }
  60.  
  61. @Override
  62. public void onFailure(Call<String> call, Throwable t) {
  63. System.out.println(t.getMessage());
  64. System.out.println("Failed");
  65. }
  66. });
  67. return resultImage;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement