Reputation: 13
I asked this question so many times and nobody answers me I don not know why, and everybody ignored it, please I need help with bitmaps, I have tried so many ways that I saw on the Internet but none solved my problem. I want to compress an image which was selected from gallery before sending it with retrofit. I saw so many ways the decode the image into string or byteArray ... etc but none worked with me. Please help me with this, my app is almost done except this issue.
I tried to convert the bitmap to a FILE: Didn't work. I tried to convert the bitmap to a BYTEARRAY: Didn't work. I tried to convert the bitmap to a STRING : Didn't work.
public byte[] getFileDataFromDrawable(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
I expect to shrink the size of the image before sending it to the server using retrofit2
Upvotes: 1
Views: 105
Reputation: 165
public void Upload_ProfilePic(String filepath) {
CommonMethods.isProgressShowNoMessage(getActivity());
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
// Change base URL to your upload server URL.
RetrofitInterface service = new Retrofit.Builder().baseUrl(RetrofitInterface.Base_Url).client(client).build().create(RetrofitInterface.class);
File file = new File(filepath);
MultipartBody.Part body;
RequestBody requestFile;
RequestBody name;
if (filepath.contains("http")) {
requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), "");
body = MultipartBody.Part.createFormData("fileContent", "", null);
name = RequestBody.create(MediaType.parse("text/plain"), "");
} else {
requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
body = MultipartBody.Part.createFormData("fileContent", file.getName(), requestFile);
name = RequestBody.create(MediaType.parse("text/plain"), file.getName());
}
RequestBody patientId = RequestBody.create(MediaType.parse("text/plain"), MainActivity.getFromSP(PreferencesKey.patientid));
retrofit2.Call<okhttp3.ResponseBody> req = service.uploadFile(name, patientId, body);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// Do Something
CommonMethods.isProgressHide();
try {
Gson gson = new Gson();
ImageUploadResponce imageUploadResponce = gson.fromJson(response.body().string(), ImageUploadResponce.class);
if (imageUploadResponce.getStatus().equals("1")) {
MainActivity.setidInSP(PreferencesKey.ProfileImage, imageUploadResponce.getPath());
Intent UpdateUI = new Intent("UpdateUI");
getActivity().sendBroadcast(UpdateUI);
CommonMethods.confirmationDialog(getActivity(), "Profile pic has been successfully updated.", "2");
} else {
CommonMethods.confirmationDialog(getActivity(), "Please try again!", "3");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
CommonMethods.isProgressHide();
CommonMethods.confirmationDialog(getActivity(), "Please try again!", "3");
}
});
}
Upvotes: 1
Reputation: 129
You can resize your bitmap with this code
private static Bitmap getResizedBitmap(Bitmap image, int IMAGE_MAX_SIZE) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = IMAGE_MAX_SIZE;
height = (int) (width / bitmapRatio);
} else {
height = IMAGE_MAX_SIZE;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
Upvotes: 1
Reputation: 785
use a third party library to compress the file. I used to work with this one https://github.com/Sunzxyong/Tiny , and I got good results.
Upvotes: 1
Reputation: 165
Use implementation 'com.kbeanie:image-chooser-library:1.5.8@aar' for get image from gallery. chooser create orignal,small and toublen. you can upload thub or small image image from path.
Upvotes: 1