当前位置: 移动技术网 > IT编程>移动开发>Android > Android如何调用系统相机拍照

Android如何调用系统相机拍照

2019年07月24日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了android调用系统相机拍照的具体代码,供大家参考,具体内容如下

/**
   * 调用系统相机
   */
  private void takephoto() {
    uri uri = null;
    if (which_image == front_image) {
      frontfile = new file(getsdpath() +"/test/front_" + getdate() + ".jpg");
      uri = uri.fromfile(frontfile);
    } else if (which_image == back_image) {
      backfile = new file(getsdpath() + "/test/back_" + getdate() + ".jpg");
      uri = uri.fromfile(backfile);
    }
    intent captureintent = new intent(mediastore.action_image_capture);
    captureintent.putextra(mediastore.images.media.orientation, 0);
    captureintent.putextra(mediastore.extra_output, uri);
    captureintent.putextra("return-data", true);
    startactivityforresult(captureintent, take_photo);
  }

  /**
   * 获取系统时间
   *
   * @return
   */
  public static string getdate() {
    calendar ca = calendar.getinstance();
    int year = ca.get(calendar.year);      // 获取年份
    int month = ca.get(calendar.month);     // 获取月份
    int day = ca.get(calendar.date);      // 获取日
    int minute = ca.get(calendar.minute);    // 分
    int hour = ca.get(calendar.hour);      // 小时
    int second = ca.get(calendar.second);    // 秒

    string date = "" + year + (month + 1) + day + hour + minute + second;

    return date;
  }

  /**
   * 获取sd path
   *
   * @return
   */
  public string getsdpath() {
    file sddir = null;
    boolean sdcardexist = environment.getexternalstoragestate()
        .equals(android.os.environment.media_mounted); // 判断sd卡是否存在
    if (sdcardexist) {
      sddir = environment.getexternalstoragedirectory();// 获取跟目录
      return sddir.tostring();
    }

    return null;
  }

  @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    super.onactivityresult(requestcode, resultcode, data);
    if (requestcode == take_photo && resultcode == result_ok) {
//      bundle bundle = data.getextras();
//      bitmap bitmap = (bitmap) bundle.get("data");
//      if (which_image == front_image) {
//        ivcardfront.setimagebitmap(bitmap);
//      } else if (which_image == back_image) {
//        ivcardback.setimagebitmap(bitmap);
//      }
      try {
        if (which_image == front_image) {

          uri uri = uri.parse(mediastore.images.media.insertimage(getcontentresolver(), frontfile.getabsolutepath(), null, null));
          contentresolver contentresolver = this.getcontentresolver();
          bitmap bitmap = bitmapfactory.decodestream(contentresolver.openinputstream(uri));
          ivcardfront.setimagebitmap(bitmap);
        } else if (which_image == back_image) {
          uri uri = uri.parse(mediastore.images.media.insertimage(getcontentresolver(), backfile.getabsolutepath(), null, null));
          contentresolver contentresolver = this.getcontentresolver();
          bitmap bitmap = bitmapfactory.decodestream(contentresolver.openinputstream(uri));
          ivcardback.setimagebitmap(bitmap);
        }
      } catch (filenotfoundexception e) {
        e.printstacktrace();
      }
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网