上传到服务器之前,如何在android中调整相机图像或图库图像的大小/压缩? [英] How to resize/compress a camera image or gallery image in android before upload to server?

查看:63
本文介绍了上传到服务器之前,如何在android中调整相机图像或图库图像的大小/压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须上传可以从图库中选择或通过相机拍摄的图像.我已经成功地做到了.但是问题是,有些时候图像大小是1MB.因此,上传到服务器需要花费更多时间.上传前,我需要调整此图片的大小.该怎么做?

I have to upload image which may be choose from gallery or take by camera. I have done this successfully. But problem is , some time image size is 1MB. So it takes more time to upload to server. I need to resize this image before upload. How to do this?

 public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    AlertDialog alertDialog1;

    if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
        try {
            FileInputStream in = new FileInputStream(destination);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10;
            imagePath = destination.getAbsolutePath();
            Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
            //img = (ImageView) findViewById(R.id.imageView1);
            int nh = (int) ( bmp.getHeight() * (512.0 / bmp.getWidth()) );
            Bitmap scaled = Bitmap.createScaledBitmap(bmp, 512, nh, true);
            img.setImageBitmap(scaled);
        } catch (Exception e) {
            // AlertDialog alertDialog1;
            alertDialog1 = new AlertDialog.Builder(getActivity()).create();
            alertDialog1.setTitle("Message");
            alertDialog1.setMessage("bala \t "+e.toString());
            alertDialog1.show();
        }

    }else if (requestCode == 1) {
        try {
            Uri selectedImageUri = data.getData();
            imagePath =  getPath(selectedImageUri);
            destination = new File(imagePath);
            FileInputStream in = new FileInputStream(destination);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10;
            imagePath = destination.getAbsolutePath();
            Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
            //int nh = (int) ( bmp.getHeight() * (512.0 / bmp.getWidth()) );
           // Bitmap scaled = Bitmap.createScaledBitmap(bmp, 512, nh, true);
            img.setImageBitmap(bmp);

            //img.setImageBitmap(bmp);
        } catch (Exception e) {
            // AlertDialog alertDialog1;
            alertDialog1 = new AlertDialog.Builder(getActivity()).create();
            alertDialog1.setTitle("Message");
            alertDialog1.setMessage("sang\t"+e.toString());
            alertDialog1.show();
        }
    }
    else{

    }
}

现在,如何调整图像的大小?

Now, how to resize the image?

推荐答案

  1. 要调整图像大小,可以使用以下功能

  1. To resize image you can use the following function

位图yourBitmap;调整位图大小= Bitmap.createScaledBitmap(yourBitmap,newWidth,newHeight,true);

压缩图像

位图bmp = BitmapFactory.decodeFile(miFoto)ByteArrayOutputStream bos = new ByteArrayOutputStream();bmp.compress(CompressFormat.JPEG,70,bos);InputStream in = new ByteArrayInputStream(bos.toByteArray());ContentBody foto =新的InputStreamBody(在"image/jpeg","filename"中);

这篇关于上传到服务器之前,如何在android中调整相机图像或图库图像的大小/压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆