我问了这个问题很多次,没有人回答我不知道为什么,每个人都忽略了它,请我需要位图方面的帮助,我已经尝试了很多我在互联网上看到的方法,但没有一种能解决我的问题。我想压缩从图库中选择的图像,然后再进行改造。我看到了将图像解码为字符串或字节数组的多种方法......等等,但没有一个和我一起工作。请帮我解决这个问题,除了这个问题,我的应用程序几乎完成了。
我尝试将位图转换为文件:不起作用。我尝试将位图转换为字节数组:不起作用。我尝试将位图转换为字符串:不起作用。
public byte[] getFileDataFromDrawable(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
我希望在使用 retrofit2 将图像发送到服务器之前缩小图像的大小
使用实现"com.kbeanie:image-chooser-library:1.5.8@aar",用于从图库中获取图像。选择器创建原始,小和图布伦。您可以从 PATH 上传 thub 或小图像图像。
使用第三方库来压缩文件。我曾经使用过这个 https://github.com/Sunzxyong/Tiny,我得到了很好的结果。
您可以使用此代码调整位图的大小
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);
}
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");
}
});
}