Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.maxim.picoto.helpers;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.util.Base64;
- import android.util.Log;
- import com.example.maxim.picoto.data.AsyncSendImageData;
- import com.example.maxim.picoto.interfaces.ServerService;
- import com.example.maxim.picoto.utils.FileUtils;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- //import org.apache.commons.codec.binary.Base64;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import okhttp3.MediaType;
- import okhttp3.MultipartBody;
- import okhttp3.RequestBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- import static com.example.maxim.picoto.MainActivity.FILES_DIR;
- public class HttpServerHelper {
- private static Bitmap resultImage;
- public static Bitmap sendImage(File file, int styleType) {
- Gson gson = new GsonBuilder()
- .setLenient()
- .create();
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://194.87.103.212:4567/")
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- ServerService api = retrofit.create(ServerService.class);
- MultipartBody.Part filePart = MultipartBody.Part.
- createFormData("file", file.getName(),
- RequestBody.create(MediaType.parse("image/*"), file));
- Call<String> call = api.upload(filePart);
- call.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- if (response.body() != null) {
- String s = response.body();
- byte[] bytes = Base64.decode(s, Base64.DEFAULT);
- resultImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- System.out.println(response.body());
- }
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- System.out.println(t.getMessage());
- System.out.println("Failed");
- }
- });
- return resultImage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement