Implementation Method of Cutting Android Call System

  • 2021-08-21 21:18:04
  • OfStack

Android calls system tailoring, which is already familiar. However, there are some minor problems when using it in recent projects, so I will sort it out here for records.

Look at the code first:


Intent intent1 = new Intent("com.android.camera.action.CROP");
      intent1.setDataAndType(Uri.fromFile(new File(image.path)), "image/*");
      intent1.putExtra("crop", "true");
      intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));//
      intent1.putExtra("aspectX", 1);
      intent1.putExtra("aspectY", 1);
      intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG);
      intent1.putExtra("outputX", 720);
      intent1.putExtra("outputY", 720);
      intent1.putExtra("return-data", false);
      startActivityForResult(intent1, 0x222);

This is the part before the modification. It seems that there is no problem and the cutting can be carried out normally. Moreover, the output size of 720*720 is relatively small, but there is a problem. When only one small piece is used to crop the picture, a black box appears around the picture.

I didn't understand how to add the black box at first, but it was added after being processed by the server (the pictures uploaded to the server by the server were compressed, and 1 thumbnail was displayed). However, after careful examination, I found that the picture had a black box before I uploaded it, so the problem naturally lies with me.

After checking a lot of information, I found the problem after all. Because when cutting large pictures or high-definition pictures, no matter how small they are, there will be no black boxes, which naturally thinks that there will be automatic filling of edges after cutting.

Know the problem, it is good to solve, and then find a solution, like idea 1, that is, when the picture is too small after cropping, do fill stretching. Later, I found this attribute, which is like this:


intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);

No explanation here, English code is practical meaning, look at literal meaning almost can understand.

After using these two important attributes, my clipping method is OK, and the modified code looks like this:


Intent intent1 = new Intent("com.android.camera.action.CROP");
      intent1.setDataAndType(Uri.fromFile(new File(image.path)), "image/*");
      intent1.putExtra("crop", "true");
      intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));//
      intent1.putExtra("aspectX", 1);
      intent1.putExtra("aspectY", 1);
      intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG);
      intent1.putExtra("outputX", 720);
      intent1.putExtra("outputY", 720);
      intent1.putExtra("scale", true);
      intent1.putExtra("scaleUpIfNeeded", true);
      intent1.putExtra("return-data", false);
      startActivityForResult(intent1, 0x222);

Because this sentence is added to the code:


intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));//

That is, the target file is specified, so tempFile can be directly used in onActivityResult.


Related articles: