欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-14 15:27:40
本文实例讲述了android编程实现调用相册、相机及拍照后直接裁剪的方法。分享给大家供大家参考,具体如下: package com.cvte.health.pho...

本文实例讲述了android编程实现调用相册、相机及拍照后直接裁剪的方法。分享给大家供大家参考,具体如下:

package com.cvte.health.phone;
import java.io.file;
import java.text.simpledateformat;
import java.util.date;
import android.app.activity;
import android.content.contentresolver;
import android.content.contenturis;
import android.content.intent;
import android.database.cursor;
import android.graphics.bitmap;
import android.graphics.drawable.drawable;
import android.net.uri;
import android.os.bundle;
import android.os.environment;
import android.provider.mediastore;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.imageview;
import com.cvte.health.accountmanager;
import com.cvte.health.healthapplication;
import com.cvte.health.r;
import com.cvte.health.api.imageupdater;
import com.cvte.health.database.user;
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) {
    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(new onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent = new intent("android.media.action.image_capture");
        mcurrentphotofile = new file("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(new onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent = new intent(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(new onclicklistener() {
      @override
      public void onclick(view v) {
        finish();
      }
    });
    mbuttonsave.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent = new intent(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
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    if (requestcode == 10 && resultcode == activity.result_ok) {
      mimageview.setimagedrawable(drawable.createfrompath(new file(
          environment.getexternalstoragedirectory(), "camera.jpg")
          .getabsolutepath()));
    } else if (requestcode == 11 && resultcode == activity.result_ok) {
      bitmap camerabitmap = (bitmap) data.getextras().get("data");
      mimageview.setimagebitmap(camerabitmap);
    } else if (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(new intent(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();
        long id = cursor.getlong(0);
        uri = contenturis.withappendedid(imguri, id);
      }
      final intent intent = new intent("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);
    } else if (requestcode == 3 && resultcode == activity.result_ok) {
      bitmap camerabitmap = (bitmap) data.getextras().get("data");
      mimageview.setimagebitmap(camerabitmap);
    }
  }
  private string getphotofilename() {
    date date = new date(system.currenttimemillis());
    simpledateformat dateformat = new simpledateformat(
        "'img'_yyyymmdd_hhmmss");
    return dateformat.format(date) + ".jpg";
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android拍照与图片处理技巧总结》、《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: