1.第一步在androidmanifest。xml中注册

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2.第二步创建activity_creama.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context="com.cqytjr.www.networkreceiver.CramaActivity"><Buttonandroid:id="@+id/btn_creama"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="@string/hello_world"tools:context=".CramaActivity" /><ImageViewandroid:id="@+id/img_creama"android:layout_width="150dip"android:layout_height="150dip"android:layout_margin="15dip"android:layout_centerHorizontal="true"android:layout_below="@+id/btn_creama"android:scaleType="fitXY"/><TextViewandroid:id="@+id/text"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/img_creama"android:textSize="16sp"android:background="#22000000"/></RelativeLayout>

3. 第三步创建cramaactivity

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class CramaActivity extends Activity {private Button creama=null;private ImageView img=null;private TextView text=null;private File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择private static final int PHOTO_REQUEST_CUT = 3;// 结果@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_crama);init();Log.i("TAG-->", ""+Environment.getExternalStorageDirectory());}private void init() {// TODO Auto-generated method stubcreama=(Button) findViewById(R.id.btn_creama);img=(ImageView) findViewById(R.id.img_creama);creama.setOnClickListener(listener);text=(TextView) findViewById(R.id.text);}protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用startPhotoZoom(Uri.fromFile(tempFile));break;case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时// 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同if (data != null)startPhotoZoom(data.getData());break;case PHOTO_REQUEST_CUT:// 返回的结果if (data != null)// setPicToView(data);sentPicToNext(data);break;}super.onActivityResult(requestCode, resultCode, data);}private OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 指定调用相机拍照后照片的储存路径cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO);}};private void startPhotoZoom(Uri uri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");// crop为true是设置在开启的intent中设置显示的view可以剪裁intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX,outputY 是剪裁图片的宽高intent.putExtra("outputX", 300);intent.putExtra("outputY", 300);intent.putExtra("return-data", true);intent.putExtra("noFaceDetection", true);startActivityForResult(intent, PHOTO_REQUEST_CUT);}// 将进行剪裁后的图片传递到下一个界面上private void sentPicToNext(Intent picdata) {Bundle bundle = picdata.getExtras();if (bundle != null) {Bitmap photo = bundle.getParcelable("data");if (photo==null) {img.setImageResource(R.drawable.ic_launcher);}else {img.setImageBitmap(photo);
//                设置文本内容为    图片绝对路径和名字text.setText(tempFile.getAbsolutePath());}ByteArrayOutputStream baos = null;try {baos = new ByteArrayOutputStream();photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);byte[] photodata = baos.toByteArray();System.out.println(photodata.toString());// Intent intent = new Intent();// intent.setClass(RegisterActivity.this, ShowActivity.class);// intent.putExtra("photo", photodata);// startActivity(intent);// finish();} catch (Exception e) {e.getStackTrace();} finally {if (baos != null) {try {baos.close();} catch (Exception e) {e.printStackTrace();}}}}}// 使用系统当前日期加以调整作为照片的名称private String getPhotoFileName() {Date date = new Date(System.currentTimeMillis());SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");return dateFormat.format(date) + ".jpg";}
}

第二种方式,我们不需要剪裁,直接用:

androidmanifest注册

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

第二部:

xml文件添加一个imageview

 <ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/ic_launcher"android:id="@+id/imageView"/>

activity代码

public class MainActivity extends Activity {GridView gridView;ImageView imageView;private Bitmap bitmap;final  static  int REQUEST_CODE_PICK_IMAGE = 1;final  static  int REQUEST_CODE_CAPTURE_CAMEIA = 2;protected void getImageFromAlbum() {Intent intent = new Intent(Intent.ACTION_PICK);intent.setType("image/*");//相片类型startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);}protected void getImageFromCamera() {String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);}else {Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);//        gridView = (GridView)findViewById(R.id.grid_view);imageView = (ImageView) findViewById(R.id.imageView);imageView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {getImageFromAlbum();}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_CODE_PICK_IMAGE) {Uri uri = data.getData();
//            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);try {Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);imageView.setImageBitmap(bitmap);} catch (IOException e) {e.printStackTrace();}} else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {Bundle extras = data.getExtras();Bitmap map = (Bitmap)extras.get("data");imageView.setImageBitmap(map);} }}

转载于:https://www.cnblogs.com/hualuoshuijia/p/6911435.html

android 打开系统相机,相关推荐

  1. android开启照相功能,Android打开系统相机并拍照的2种显示方法

    本文实例为大家分享了Android打开系统相机并拍照的具体实现代码,供大家参考,具体内容如下 目标效果: 第二张为点击第一个按钮拍照后显示的,比较模糊,第三章为点击第二个按钮拍照后显示的,比较清楚. ...

  2. android打开系统相机分别获得原图和缩略图

    第一种:获得缩略图: 打开相机 private void photo() {Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_C ...

  3. Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto)void onclick() {Intent intent = new Intent(MediaSt ...

  4. android固定位置拍照,Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto) void onclick() { Intent intent = new Intent(Media ...

  5. android 调用系统相机拍照 获取原图

    博客源址:android 调用系统相机拍照 获取原图 博客时间:2013-04-23 11:08 好吧,为了这个问题又折腾了一整天.之前在网上找来的方法,如果在onActivityResult中直接用 ...

  6. Android调用系统相机拍照

    参考: Taking Photos Simply FileProvider 项目地址(好多人找我要,我传到百度云啦,大家自取):链接:https://pan.baidu.com/s/1nWsoE0eS ...

  7. android 实现自动拍照,Android:调用系统相机实现拍照+裁切(兼容7.0以上系统)

    android.jpg 前言 在平时的工作中,我们会经常遇到这样的需求:比如更改用户头像,我们就需要打开相机或相册,选择照片后进行裁剪,最后上传给后台... 相信这样的功能对小伙伴们来说可能早已司空见 ...

  8. Android调用系统相机和相册(更换微信头像)

    最近做了调用系统相机和相册,在其他博客中看到还有对图像进行剪切,大家都知道,我们在玩微信的时候,头像更换是方形图片,接下来我们就对这种情况具体进行描述: 必要的权限: <uses-permiss ...

  9. Android 调用系统相机拍照和录制视频,保存照片和视频

    1.申请权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ...

最新文章

  1. C语言 数组(做个笔记)
  2. Windows CE 程序设计 (3rd 版)
  3. swift学习之set和get方法
  4. python语言有几种编程方式_零基础自学python语言,有哪几种方法?龟叔说:这套教程带你入门...
  5. Curator实现分布式锁的基本原理-getTheLock
  6. java 数组 length 减少_java中数组有没有length()方法?string没有lenght()方法?
  7. 航行金税盘_通过陌生事物的情感进行统计好奇心航行
  8. python turtle_Python:turtle画粽子
  9. EXCEL 宏 拆分数据表 VBA 视频教程下载
  10. Latex 爬过的坑(4)——Latex中插入Emoji
  11. 十八掌教育_徐培成_Hadoop3.0-01.简介
  12. 聊聊Ping命令的返回值Pong
  13. 怎么去除脸上的黄褐斑
  14. matlab按图像边缘抠图_有哪些高效的抠图方法?
  15. SSD,eMMC,UFS的区别
  16. Wall following algorithm机器人沿墙算法(一)
  17. 【GNN】循序渐进认识SSL
  18. 【华人学者风采】黄维 西北工业大学
  19. 利用幂等性区分HTTP的POST与PUT请求
  20. 树莓派官方摄像头detected=0问题

热门文章

  1. c语言equal,C ++中的ratio_equal()示例
  2. sql 2008找不到服务器,sql server 2005 数据库迁移问题总结——错误 ‘80004005’ 在 sys.servers 中找不到服务器 ‘XXX’...
  3. JAVA面试题------------final 关键字是干什么用的?谈谈你的理解。
  4. SpringCloud和SprigBoot之间的依赖关系怎么看
  5. 蜗杆参数法设计_齿轮几何尺寸设计,很实用的Excel表格(附自动计算表格)
  6. 17 张程序员专属壁纸,有你喜欢的吗?
  7. 硬件:RS232基础知识笔记
  8. Linux文本处理tr命令笔记
  9. windows安全模式_Winclone 8 for Mac(Windows分区备份迁移和还原工具)
  10. 数值的加减会改变python中id,在python中调用Nan值并更改为数字