我从图库或照相机中选择一个文件。然后,我将它们上传到服务器。但是我无法缩小它们的尺寸。图像质量无关紧要。你能告诉我最好的方法吗?我是一个初学者,我不知道如何使用代码。因此,请提供详细信息。
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.myapplication.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Glide.with(this).load(currentPhotoPath).into(iv);
} else if (requestCode == SELECT_A_PHOTO && resultCode == RESULT_OK){
selectedPhoto = data.getData();
Glide.with(this).load(selectedPhoto).into(iv);
}
private void galleryIntent()
{
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,SELECT_A_PHOTO);
}
答案 0 :(得分:0)
我找到了答案。这种方法可让您减小尺寸。
// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
答案 1 :(得分:0)
此函数接受图像路径并将其转换为位图。 700是我在此处设置的高度/宽度的基本阈值。您可以相应地更改它并创建缩放的位图(数字越小,图像尺寸越小)。 while循环的每次迭代都会将图像缩小一半。您可以根据需要对其进行修改。
private Bitmap reduce_image_to_bitmap(String file_path){
Bitmap bit_map = BitmapFactory.decodeFile(file_path);
int h = bit_map.getHeight();
int w = bit_map.getWidth();
while(h > 700 || w > 700){
h = h/2;
w = w/2;
}
Bitmap out = Bitmap.createScaledBitmap(bit_map, w, h, false);
return out;
}
确保将位图转换为文件,然后继续将文件发送到服务器。
答案 2 :(得分:0)
使用此库:Compressor
答案 3 :(得分:0)
首先,您需要处理此图像,以便减小尺寸和保持质量。您需要运行后台任务,以便在进行大图像处理时设备不会发出嘶嘶声。
然后您可以在此过程中显示进度对话框,只需将此代码添加到onCreate活动中即可。
public ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(MyProfileEidtActivity.this);
progressDialog.setMessage("Loading ...");
// just execute this process
new ImageProcessing().execute("YOUR IMAGE PATH");
}
public class ImageProcessing extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Image Processing");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... strings) {
Bitmap mainImage = null;
Bitmap converetdImage = null;
ByteArrayOutputStream bos = null;
byte[] bt = null;
String encodeString = null;
try {
mainImage = BitmapFactory.decodeFile(strings[0]);
/// 500 means image size will be maximum 500 kb
converetdImage = getResizedBitmap(mainImage, 500);
bos = new ByteArrayOutputStream();
converetdImage.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bt = bos.toByteArray();
encodeString = Base64.encodeToString(bt, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return encodeString;
}
@Override
protected void onPostExecute(String image) {
super.onPostExecute(s);
progressDialog.dismiss();
// this image will be your reduced image path
}
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}