一聚教程网:一个值得你收藏的教程网站

热门教程

Android编程实现调用相册、相机及拍照后直接裁剪的方法

时间:2022-06-25 23:30:07 编辑:袖梨 来源:一聚教程网

 

 代码如下复制代码

packagecom.cvte.health.phone;

importjava.io.File;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importandroid.app.Activity;

importandroid.content.ContentResolver;

importandroid.content.ContentUris;

importandroid.content.Intent;

importandroid.database.Cursor;

importandroid.graphics.Bitmap;

importandroid.graphics.drawable.Drawable;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.os.Environment;

importandroid.provider.MediaStore;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

importcom.cvte.health.AccountManager;

importcom.cvte.health.HealthApplication;

importcom.cvte.health.R;

importcom.cvte.health.api.ImageUpdater;

importcom.cvte.health.database.User;

publicclassChangePortraitActivityextendsActivity {

  privateImageView mImageView;

  privateButton mButtonCamera;

  privateButton mButtonPhoto;

  privateButton mButtonBack;

  privateButton mButtonSave;

  privateUser mUser =null;

  privateFile mCurrentPhotoFile;

  @Override

  publicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_change_portrait);

    HealthApplication.getInstance().addActivity(this);

    mUser = AccountManager.getInstance().getCurrentUser();

    mImageView = (ImageView)this.findViewById(R.id.imageview_preview);

    mButtonCamera = (Button)this.findViewById(R.id.button_cameraButton);

    mButtonPhoto = (Button)this.findViewById(R.id.button_photoButton);

    mButtonBack = (Button) findViewById(R.id.button_back);

    mButtonSave = (Button) findViewById(R.id.button_save);

    ImageUpdater.getInstance(this).updateUserPhoto(mUser, mImageView);

    mButtonCamera.setOnClickListener(newOnClickListener() {

      @Override

      publicvoidonClick(View v) {

        Intent intent =newIntent("android.media.action.IMAGE_CAPTURE");

        mCurrentPhotoFile =newFile("mnt/sdcard/DCIM/Camera/",

            getPhotoFileName());

        intent.putExtra(MediaStore.EXTRA_OUTPUT,

            Uri.fromFile(mCurrentPhotoFile));

        startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);

        /*

         * Intent intent = new

         * Intent("android.media.action.IMAGE_CAPTURE");

         * intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new

         * File(Environment .getExternalStorageDirectory(),

         * "camera.jpg")));

         * intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

         * startActivityForResult(intent, 10);

         */

      }

    });

    mButtonPhoto.setOnClickListener(newOnClickListener() {

      @Override

      publicvoidonClick(View v) {

        Intent intent =newIntent(Intent.ACTION_GET_CONTENT);

        intent.addCategory(Intent.CATEGORY_OPENABLE);

        intent.setType("image/*");

        intent.putExtra("crop","true");

        intent.putExtra("aspectX",1);

        intent.putExtra("aspectY",1);

        intent.putExtra("outputX",300);

        intent.putExtra("outputY",300);

        intent.putExtra("return-data",true);

        startActivityForResult(intent,11);

      }

    });

    mButtonBack.setOnClickListener(newOnClickListener() {

      @Override

      publicvoidonClick(View v) {

        finish();

      }

    });

    mButtonSave.setOnClickListener(newOnClickListener() {

      @Override

      publicvoidonClick(View v) {

        Intent intent =newIntent(ChangePortraitActivity.this,

            UserProfileActivity.class);

        mImageView.setDrawingCacheEnabled(Boolean.TRUE);

        intent.putExtra("BITMAP", mImageView.getDrawingCache());// 这里可以放一个bitmap

        startActivity(intent);

        finish();

        overridePendingTransition(R.anim.activity_in_from_left,

            R.anim.activity_out_from_right);

      }

    });

  }

  @Override

  protectedvoidonActivityResult(intrequestCode,intresultCode, Intent data) {

    if(requestCode ==10&& resultCode == Activity.RESULT_OK) {

      mImageView.setImageDrawable(Drawable.createFromPath(newFile(

          Environment.getExternalStorageDirectory(),"camera.jpg")

          .getAbsolutePath()));

    }elseif(requestCode ==11&& resultCode == Activity.RESULT_OK) {

      Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");

      mImageView.setImageBitmap(cameraBitmap);

    }elseif(requestCode ==1&& resultCode == Activity.RESULT_OK) {

      Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

      ContentResolver cr = ChangePortraitActivity.this

          .getContentResolver();

      Uri fileUri = Uri.fromFile(mCurrentPhotoFile);

      sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,

          fileUri));

      try{

        Thread.sleep(3000);

      }catch(InterruptedException e) {

        e.printStackTrace();

      }

      Cursor cursor = cr.query(imgUri,null,

          MediaStore.Images.Media.DISPLAY_NAME +"='"

              + mCurrentPhotoFile.getName() +"'",null,null);

      Uri uri =null;

      if(cursor !=null&& cursor.getCount() >0) {

        cursor.moveToLast();

        longid = cursor.getLong(0);

        uri = ContentUris.withAppendedId(imgUri, id);

      }

      finalIntent intent =newIntent("com.android.camera.action.CROP");

      intent.setDataAndType(uri,"image/*");

      intent.putExtra("crop","true");

      intent.putExtra("aspectX",1);

      intent.putExtra("aspectY",1);

      intent.putExtra("outputX",300);

      intent.putExtra("outputY",300);

      intent.putExtra("return-data",true);

      ChangePortraitActivity.this.startActivityForResult(intent,3);

    }elseif(requestCode ==3&& resultCode == Activity.RESULT_OK) {

      Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");

      mImageView.setImageBitmap(cameraBitmap);

    }

  }

  privateString getPhotoFileName() {

    Date date =newDate(System.currentTimeMillis());

    SimpleDateFormat dateFormat =newSimpleDateFormat(

        "'IMG'_yyyyMMdd_HHmmss");

    returndateFormat.format(date) +".jpg";

  }

}

 

热门栏目