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"> <Button
android: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" /> <ImageView
android: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" />
<TextView
android: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;// 结果 @Override
public 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 stub creama=(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(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub Intent 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

 <ImageView
android: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();
}
}
@Override
protected 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() {
@Override
public void onClick(View v) {
getImageFromAlbum();
}
}); } @Override
protected 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); } } }

android 打开系统相机,的更多相关文章

  1. Android调用系统相机、自己定义相机、处理大图片

    Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...

  2. Android调用系统相机、自定义相机、处理大图片

    Android调用系统相机和自定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显示出来,该例子也会涉及到Android加载大图片时候的处理 ...

  3. Android调用系统相机功能

    在常规应用开发过程中,我们经常会使用到手机的相机功能,通过调用系统相机方便快捷的帮助我们实现拍照功能,本篇我将带领大家实现一下,如何通过调用系统相机实现拍照. 第一种:调用系统相机拍照,通过返回的照片 ...

  4. android调用系统相机并获取图片

    如果不是特别的要求,通过拍照的方式取得图片的话,我们一般调用系统的拍照来完成这项工作,而没必要再自己去实现一个拍照功能.调用系统相机很简单,只需要一个intent就可以跳转到相几界面,然后再通过onA ...

  5. Android 打开系统相册和系统视

    1.打开系统相册 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("vnd.android.cursor.dir ...

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

      好吧,为了这个问题又折腾了一整天.之前在网上找来的方法,如果在onActivityResult中直接用data.getData()的方式来生成bitmap,其实获取的是拍照生成的缩略图!看看尺寸就 ...

  7. Android 调用系统相机拍照保存以及调用系统相册的方法

    系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new ...

  8. Android调用系统相机和相册并解决data为空,OOM,图片角度不对的问题

    最近公司项目用到手机拍照的问题,好不容易在网上copy了一些代码,但是运行起来一大堆bug,先是三星手机上运行程序直接崩掉,debug了一下原来是onActivityResult中data返回为空,找 ...

  9. android 调用系统相机

    // 调用相机拍照的请求码 public static final int REQUEST_TAKE_PHOTO_CODE = 1; public static final int REQUEST_T ...

随机推荐

  1. 为JDK自带的jvisualvm安装Visual GC插件

    1.打开cmd,输入jvisualvm,回车: 2.点击工具——>插件; 3.访问网址:https://visualvm.github.io/pluginscenters.html,找到自己JD ...

  2. Javascript学习笔记--理解prototype

    prototype和closure是js中两个不好搞懂的概念,幸好网上有很多相关的文章,在网上查了一遍以后,总是是觉得有点理解了.今天先说说prototype. 之前一直被ajax in action ...

  3. 向Windows内核驱动传递用户层定义的事件Event,并响应内核层的通知

    完整的程序在下载:http://download.csdn.net/detail/dijkstar/7913249 用户层创建的事件Event是一个Handle句柄,和内核中的创建的内核模式下的KEV ...

  4. Linux线程编程之生产者消费者问题

    前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注意的事项.文中涉及的代码运行环境如下: 本文假定读者已具备线程同步的基础知识. 一  顺序表循环队列 1.1 ...

  5. SQL 查询结果保存为 临时表

    -- 1. 在使用select into前,可以先做一下判断 if OBJECT_ID('tempdb..#TT')is not NULL drop table #TT -- 2. 查询结果保存为临时 ...

  6. c++ map使用问题【运行结果不一样】

    map经常把指针作为key,这种情况下. 我们经常会很自然的以为,如果要取元素时,会按照我们存的顺序拿到元素. 但是事实上不是这样的,因为map取得时候是按key的大小排序的,而如果用指针作为key, ...

  7. dirname的用法:获取文件的父级目录路径

    命令:dirname 获取文件的路径(到父级目录)用法:dirname file_name [root@bogon opt]# a=$(dirname /mnt/a/b/c/d/a.sh) [root ...

  8. Android服务开发——WebService

    我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...

  9. VC/MFC程序开启关闭和打开自己或其他软件,更改窗口类

    一. 关闭自身软件 直接在需要关闭的位置输入 HANDLE hself = GetCurrentProcess(); TerminateProcess(hself, 0); 二.关闭其他软件 流程: ...

  10. elk单台环境搭建

    一.简介1.核心组成ELK由Elasticsearch.Logstash和Kibana三部分组件组成:Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分 ...