SoFunction
Updated on 2025-04-10

Android programming implements the method of calling albums, cameras and direct cropping after taking photos

This article describes the Android programming method to directly crop the album, camera and take photos. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ChangePortraitActivity extends Activity {
  private ImageView mImageView;
  private Button mButtonCamera;
  private Button mButtonPhoto;
  private Button mButtonBack;
  private Button mButtonSave;
  private User mUser = null;
  private File mCurrentPhotoFile;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_change_portrait);
    ().addActivity(this);
    mUser = ().getCurrentUser();
    mImageView = (ImageView) (.imageview_preview);
    mButtonCamera = (Button) (.button_cameraButton);
    mButtonPhoto = (Button) (.button_photoButton);
    mButtonBack = (Button) findViewById(.button_back);
    mButtonSave = (Button) findViewById(.button_save);
    (this).updateUserPhoto(mUser, mImageView);
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(".IMAGE_CAPTURE");
        mCurrentPhotoFile = new File("mnt/sdcard/DCIM/Camera/",
            getPhotoFileName());
        (MediaStore.EXTRA_OUTPUT,
            (mCurrentPhotoFile));
        startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);
        /*
         * Intent intent = new
         * Intent(".IMAGE_CAPTURE");
         * (MediaStore.EXTRA_OUTPUT, (new
         * File(Environment .getExternalStorageDirectory(),
         * "")));
         * (MediaStore.EXTRA_VIDEO_QUALITY, 0);
         * startActivityForResult(intent, 10);
         */
      }
    });
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        (Intent.CATEGORY_OPENABLE);
        ("image/*");
        ("crop", "true");
        ("aspectX", 1);
        ("aspectY", 1);
        ("outputX", 300);
        ("outputY", 300);
        ("return-data", true);
        startActivityForResult(intent, 11);
      }
    });
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();
      }
    });
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(,
            );
        ();
        ("BITMAP", ()); // You can put a bitmap here        startActivity(intent);
        finish();
        overridePendingTransition(.activity_in_from_left,
            .activity_out_from_right);
      }
    });
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
      ((new File(
          (), "")
          .getAbsolutePath()));
    } else if (requestCode == 11 && resultCode == Activity.RESULT_OK) {
      Bitmap cameraBitmap = (Bitmap) ().get("data");
      (cameraBitmap);
    } else if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
      Uri imgUri = .EXTERNAL_CONTENT_URI;
      ContentResolver cr = 
          .getContentResolver();
      Uri fileUri = (mCurrentPhotoFile);
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
          fileUri));
      try {
        (3000);
      } catch (InterruptedException e) {
        ();
      }
      Cursor cursor = (imgUri, null,
          .DISPLAY_NAME + "='"
              + () + "'", null, null);
      Uri uri = null;
      if (cursor != null && () > 0) {
        ();
        long id = (0);
        uri = (imgUri, id);
      }
      final Intent intent = new Intent("");
      (uri, "image/*");
      ("crop", "true");
      ("aspectX", 1);
      ("aspectY", 1);
      ("outputX", 300);
      ("outputY", 300);
      ("return-data", true);
      (intent, 3);
    } else if (requestCode == 3 && resultCode == Activity.RESULT_OK) {
      Bitmap cameraBitmap = (Bitmap) ().get("data");
      (cameraBitmap);
    }
  }
  private String getPhotoFileName() {
    Date date = new Date(());
    SimpleDateFormat dateFormat = new SimpleDateFormat(
        "'IMG'_yyyyMMdd_HHmmss");
    return (date) + ".jpg";
  }
}

For more information about Android related content, please check out the topic of this site:Summary of Android photography and picture processing skills》、《Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.