在android中,如何在将图像上传为个人资料图片之前调整图像大小和用户裁剪图像

在android中,如何在将图像上传为个人资料图片之前调整图像大小和用户裁剪图像,android,performance,Android,Performance,我是Android的初学者,我在上传图像之前搜索了很多用户裁剪图像,就像在whattsapp中一样,还最小化了图像的字节大小,但是我没有得到任何可靠的答案来添加到我的应用程序中 public class Crop extends AppCompatActivity { private Uri picUri; File f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我是Android的初学者,我在上传图像之前搜索了很多用户裁剪图像,就像在whattsapp中一样,还最小化了图像的字节大小,但是我没有得到任何可靠的答案来添加到我的应用程序中

public class Crop extends AppCompatActivity {

 private Uri picUri;
 File f;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_camera_crop);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 Button btn_camera = (Button) findViewById(R.id.btn_camera);
 btn_camera.setOnClickListener(new View.OnClickListener() {
 @Override
   public void onClick(View v) {

 try {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = new File(android.os.Environment.getExternalStorageDirectory(), "makegifimage.jpg");
picUri = Uri.fromFile(f);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
 startActivityForResult(intent, 1);

 } catch (ActivityNotFoundException anfe) {

Toast.makeText(getApplicationContext(),"couldnt open your      camera",Toast.LENGTH_SHORT).show();

  }
  }
  });

  }


  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {
if (requestCode == 1) {

 CropImage();

} else if (requestCode == 2) {

 BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

  Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
 bitmapOptions);
    ImageView im_crop = (ImageView) findViewById(R.id.im_crop);
    im_crop.setImageBitmap(bitmap);

}
}

}

private void CropImage() {
try {

Intent cropIntent = new Intent("com.android.camera.action.CROP");
 cropIntent.setDataAndType(picUri, "image/*");
 cropIntent.putExtra("crop", "true");
 cropIntent.putExtra("aspectX", 2);
 cropIntent.putExtra("aspectY", 2);
 cropIntent.putExtra("outputX", 256);
 cropIntent.putExtra("outputY", 256);
 cropIntent.putExtra("return-data", true);
 startActivityForResult(cropIntent, 2);
}
catch (ActivityNotFoundException e) {

 Toast.makeText(this, "Your device is not supportting the crop action", Toast.LENGTH_SHORT);

}
}
}
并将按钮和图像视图添加到布局中

<ImageView
  android:id="@+id/im_crop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"/>

  <Button
     android:id="@+id/btn_camera"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Camera"
     android:layout_alignParentBottom="true" />

有两种方法。第一个是为它提供裁剪器,第二个是您可以调整位图的大小,然后将其发送到服务器。在第二种情况下,用户将不知道图像的哪一部分(可能是中间)被裁剪并上传为他/她的个人资料图片。因此,我将推荐第一种方法,因为这样用户将知道图像的哪一部分将被截断。 你必须知道,内置android crop应用程序很难使用,因为它在不同的设备上显示不同的行为。所以你必须使用第三方crop库,因为你可以创建自己的裁剪器。下面是一些您可以使用的有用的裁剪库。在github上有很多库,您可以查找它们


使用这个库txz,它非常有用,很乐意帮助!yaa我使用了此代码,但它不适用于所有设备,它取决于用户手机中的应用程序,但我不希望我的应用程序依赖于其他应用程序。然后我认为你需要使用一些自定义裁剪机制。你可以尝试ADMyaa发布的答案。我正在浏览这些链接和txz