Android调用系统自带的摄像头进行拍照

一:布局文件

<LinearLayout 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:orientation="vertical" >
    <Button
        android:id="@+id/openCameraPlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拍照" />
    <ImageView
        android:id="@+id/showImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

二:主要代码

package com.example.opeancamerademo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
public class MainActivity extends Activity implements OnClickListener {
 private Button openCameraPlay;
 private ImageView showImageView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  openCameraPlay = (Button) findViewById(R.id.openCameraPlay);
  showImageView = (ImageView) findViewById(R.id.showImageView);
  openCameraPlay.setOnClickListener(this);
 }
 @Override
 public void onClick(View view) {
  // 1.这一种要设置权限才能启动(只要Action设置对了就能启动,有多种方式)
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, 1);
  // 2.这一中不要在Manifest中设置权限就能启动,这是调用系统自带的。
  // Intent intent = new Intent();
  // intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
  // startActivityForResult(intent, 1);
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == Activity.RESULT_OK) {
   Bundle bundle = data.getExtras();
   // 获取相机返回的数据,并转换为Bitmap图片格式
   Bitmap bitmap = (Bitmap) bundle.get("data");
   FileOutputStream b = null;
   File file = new File("/sdcard/testImage");
   if (!file.exists())
    file.mkdirs();
   try {
    b = new FileOutputStream("sdcard/testImage/test.jpg");
    // 把数据写入文件
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } finally {
    try {
     b.flush();
     b.close();
     b = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   showImageView.setImageBitmap(bitmap);
  }
 }
}

三:配置权限

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

转载于:https://my.oschina.net/u/1791806/blog/295105

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值