如何调整Android上拍摄的照片的大小?

如何调整Android上拍摄的照片的大小?,android,Android,我想用多部分/表单数据将我拍摄的照片发送到服务器 当然,所有的过程都进行得很顺利 我在onActivityResult上得到了图片的绝对路径。我将其发送到AsyncTask类以将其更改为文件对象。然后,我使用FileInputStream将图片发送到服务器 这是我发送图片的代码 主要活动: public void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(

我想用多部分/表单数据将我拍摄的照片发送到服务器

当然,所有的过程都进行得很顺利

我在onActivityResult上得到了图片的绝对路径。我将其发送到AsyncTask类以将其更改为文件对象。然后,我使用FileInputStream将图片发送到服务器

这是我发送图片的代码

主要活动:

 public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode != RESULT_OK)
        return;

    if(requestCode == PICK_FROM_CAMERA){
        // uri of selected picture
        imageUri = data.getData();


        // path of selected picture
        Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
        c.moveToNext();
        absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));

        Glide.with(this).load(imageUri).into(image);
    }
}
异步任务类: 绝对路径在参数中[7]

 FileInputStream fileInputStream ;
        wr.writeBytes("\r\n--" + boundary + "\r\n");
        wr.writeBytes("Content-Disposition: form-data; name=\"file1[]\"; " +
                "filename=\"image.jpg\"\r\n");
        wr.writeBytes("Content-Type: application/octet-stream\r\n\r\n");
        fileInputStream = new FileInputStream(params[7]);
当我发送图片时,我只发送它而不调整大小。 完成发送需要花费太多的时间和数据。 我想在发送到服务器时使图片变小。 也就是说,使用绝对路径,我想制作更小的图片,可以转换为文件对象


有人能给我一些提示吗?

您可以将图像文件存储为Uri变量,然后根据需要压缩的宽度和高度调整图像大小

     Bitmap b = BitmapFactory.decodeFile(String.valueOf(fileUri));
     Bitmap out = Bitmap.createScaledBitmap(b, IMG_WIDTH, IMG_HEIGHT, false);
     FileOutputStream fOut;

     try {
            fOut = new FileOutputStream(fileUri);
            out.compress(Bitmap.CompressFormat.JPEG, 60, fOut);
            fOut.flush();
            fOut.close();
            b.recycle();
            out.recycle();
            Log.i("Compressing file", String.valueOf(uriFile));
      } catch (Exception e) {
            Log.e("ErrInCompressingPicture",""  + e);
      }

您可以将imageFile存储为Uri变量,然后根据需要压缩的宽度和高度调整图像大小

     Bitmap b = BitmapFactory.decodeFile(String.valueOf(fileUri));
     Bitmap out = Bitmap.createScaledBitmap(b, IMG_WIDTH, IMG_HEIGHT, false);
     FileOutputStream fOut;

     try {
            fOut = new FileOutputStream(fileUri);
            out.compress(Bitmap.CompressFormat.JPEG, 60, fOut);
            fOut.flush();
            fOut.close();
            b.recycle();
            out.recycle();
            Log.i("Compressing file", String.valueOf(uriFile));
      } catch (Exception e) {
            Log.e("ErrInCompressingPicture",""  + e);
      }

您可以压缩图片,然后再发送到服务器

一种解决方案直接设置在SampleSize中:

  public static Bitmap getImage(String imgPath){
      BitmapFactory.Options options=new BitmapFactory.Options();
      options.inSampleSize=2;
      try {
          b=BitmapFactory.decodeFile(imgPath, options);
      } catch (Exception e) {
              e.printStackTrace();
      }
      return b;
 }
另一种解决方案是设置所需的宽度和高度:

public static Bitmap getSmallBitmap(String imgPath,int reWidth,int reHeight) {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(imgPath, options);
     int width = options.outWidth;
     int height = options.outHeight;
     int inSampleSize=0;
     if (height > reqHeight || width > reqWidth) {
         final int heightRatio = Math.round((float) height/ (float) reqHeight);
         final int widthRatio = Math.round((float) width / (float) reqWidth);
         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
     options.inSampleSize = inSampleSize;
     options.inJustDecodeBounds = false;
     return BitmapFactory.decodeFile(imgPath, options);
  }
public静态位图getSmallBitmap(字符串imgPath、int-reWidth、int-reHeight){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(imgPath,选项);
int width=options.outWidth;
int height=options.outHeight;
int inSampleSize=0;
如果(高度>要求高度| |宽度>要求宽度){
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
options.inSampleSize=inSampleSize;
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(imgPath,选项);
}

您可以在发送到服务器之前压缩图片

一种解决方案直接设置在SampleSize中:

  public static Bitmap getImage(String imgPath){
      BitmapFactory.Options options=new BitmapFactory.Options();
      options.inSampleSize=2;
      try {
          b=BitmapFactory.decodeFile(imgPath, options);
      } catch (Exception e) {
              e.printStackTrace();
      }
      return b;
 }
另一种解决方案是设置所需的宽度和高度:

public static Bitmap getSmallBitmap(String imgPath,int reWidth,int reHeight) {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(imgPath, options);
     int width = options.outWidth;
     int height = options.outHeight;
     int inSampleSize=0;
     if (height > reqHeight || width > reqWidth) {
         final int heightRatio = Math.round((float) height/ (float) reqHeight);
         final int widthRatio = Math.round((float) width / (float) reqWidth);
         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
     options.inSampleSize = inSampleSize;
     options.inJustDecodeBounds = false;
     return BitmapFactory.decodeFile(imgPath, options);
  }
public静态位图getSmallBitmap(字符串imgPath、int-reWidth、int-reHeight){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(imgPath,选项);
int width=options.outWidth;
int height=options.outHeight;
int inSampleSize=0;
如果(高度>要求高度| |宽度>要求宽度){
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
options.inSampleSize=inSampleSize;
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(imgPath,选项);
}