-4
我已经尝试了互联网上的多种解决方案,但没有运气,如何在上载应用程序时更改图像大小? 我希望它能够以上传2MB文件的方式发送到大小为50kb的服务器。 请帮我在发送到服务器之前,android会将图像大小压缩
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//get image thumbnail
if (requestCode == REQUEST_CODE_PICKER && resultCode == RESULT_OK && data != null) {
ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);
absolutePath = null;
// do your logic ....
for (Image img : images) {
Log.v(LOG_TAG, img.getName() + " " + img.getPath());
absolutePath = img.getPath();
absolutePath = String.valueOf(Compressor.getDefault(getContext()).compressToFile(imageFile));
Bundle bundleExtras = data.getExtras();
image = (Bitmap) bundleExtras.get("data");
}
consultantProfileImageView.setImageBitmap(getBitmapFromPath(absolutePath));
new UploadConsultantProfileImageTask(getContext(), absolutePath).execute();
postConsultant();
}
}
public File getBitmapFromPath(String filePath) {
File imageFile = new File(filePath);
Bitmap imageBitmap = null;
imageBitmap = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream(bos.toByteArray());
File compressedImageFile = Compressor.getDefault(getContext()).compressToFile(imageFile);
if (compressedImageFile.exists()) {
imageBitmap = BitmapFactory.decodeFile(compressedImageFile.getAbsolutePath());
}
return compressedImageFile;
}
“我试图从没有运气互联网多种解决方案” - 请解释一下你试了一下(通过[MCVE]),并具体是什么“没有运气”是指相对于你尝试过什么。 “当我上传2MB文件时” - 请详细解释**这是什么类型的文件(PNG,JPEG,RAW,GIF,WEBP等)以及您存储的文件的位置。 – CommonsWare
你想让你的应用程序压缩图像,或者你打算手动完成吗?请更好地解释你的问题.. – Rippr
我的应用程序应该允许用户添加个人资料图片然后裁剪/压缩到50kb,然后将其保存到服务器 – Fuluza