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

Android打开相机和相册实例代码

程序员文章站 2024-03-06 11:32:07
本文实例为大家分享了android打开相机和相册具体代码,供大家参考,具体内容如下 打开相机  /** * 选择相机 */...

本文实例为大家分享了android打开相机和相册具体代码,供大家参考,具体内容如下

打开相机 

 /**
   * 选择相机
   */

  private void showcamera() {
    // 跳转到系统照相机
    intent cameraintent = new intent(mediastore.action_image_capture);
    if (cameraintent.resolveactivity(getpackagemanager()) != null) {
      // 设置系统相机拍照后的输出路径
      // 创建临时文件
      mtmpfile = otherutils.createfile(getapplicationcontext());

      cameraintent.putextra(mediastore.extra_output, uri.fromfile(mtmpfile));
      startactivityforresult(cameraintent, request_camera);
    } else {
      toast.maketext(getapplicationcontext(), r.string.msg_no_camera, toast.length_short).show();
    }

  } 

打开相册

intent albumintent = new intent(intent.action_pick, null);
    albumintent.setdataandtype(mediastore.images.media.external_content_uri, "image/*");
    startactivityforresult(albumintent, request_album_ok); 

onactivityresult 

 @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    super.onactivityresult(requestcode, resultcode, data);

    switch (requestcode) {
      case request_camera:
        //请求相机
        if (mtmpfile != null) {
          log.d(tag, "onactivityresult: 请求相机: " + mtmpfile.getabsolutepath());

          picasso.with(this).load(mtmpfile).centercrop().resize(otherutils.dip2px(this,100),otherutils.dip2px(this,100))
              .error(r.mipmap.pictures_no).into(mivdispaly);
        }
        break;
      case request_album_ok:
        log.d(tag, "onactivityresult:相册 " + data.getdata().tostring());
        contentresolver resolver = getcontentresolver();

        try {
          inputstream inputstream = resolver.openinputstream(data.getdata());

          bitmap bitmap = bitmapfactory.decodestream(inputstream);

          mivdispaly.setimagebitmap(bitmap);

        } catch (filenotfoundexception e) {


          e.printstacktrace();
        }

        //        cursor query = resolver.query(data.getdata(), null, null, null, null);
//
//        string str = null;
//        while (query.movetonext()) {
//          log.d(tag, "onactivityresult:数量 " + query.getcount());
//          str =query.getstring(query.getcolumnindex(mediastore.images.media.data));
//          log.d(tag, "onactivityresult: 列名" + query.getstring(query.getcolumnindex(mediastore.images.media.data)));
//        }
//        query.close();
//        picasso.with(this).load(new file(str)).centercrop().resize(otherutils.dip2px(this,100),otherutils.dip2px(this,100))
//            .error(r.mipmap.pictures_no).into(mivdispaly);
        break;

    }
  } 

otherutils 

import android.content.context;
import android.os.environment;

import java.io.file;
import java.util.date;

/**
 * created by 刘楠 on 2016/7/20 0020.22:37
 */
public class otherutils {
  public static int dip2px(context context, float dpvalue) {
    final float scale = context.getresources().getdisplaymetrics().density;
    return (int) (dpvalue * scale + 0.5f);
  }

  public static int px2dip(context context, float pxvalue) {
    final float scale = context.getresources().getdisplaymetrics().density;
    return (int) (pxvalue / scale + 0.5f);
  }

  /**
   * 获取拍照相片存储文件
   * @param context
   * @return
   */
  public static file createfile(context context){
    file file;
    if(environment.getexternalstoragestate().equals(environment.media_mounted)){
      string timestamp = string.valueof(new date().gettime());
      file = new file(environment.getexternalstoragedirectory() +
          file.separator + timestamp+".jpg");
    }else{
      file cachedir = context.getcachedir();
      string timestamp = string.valueof(new date().gettime());
      file = new file(cachedir, timestamp+".jpg");
    }
    return file;
  }

}

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