android调用系统相机拍照保存照片并显示在当前界面

主要思路是在使用照相机拍照,然后为拍得的照片在SD卡新开一个储存照片的文件


代码:因为要调用照相机和SD卡所以需要添加以下权限:

在manifests中添加
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



activity_main.xml中


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击按钮拍照"
        />
    <ImageView
        android:id="@+id/imageView"
        android:layout_below="@id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />



</LinearLayout>


MainActivity中:


package com.example.administrator.myapplication1;

import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Environment;
import java.io.FileOutputStream;
import java.io.File;
import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import android.app.Activity;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.MediaStore;
 import android.view.View;
 import android.widget.Button;
 import android.widget.ImageView;
import android.os.Environment;

public class MainActivity extends AppCompatActivity {

    private Button mButton;
        private ImageView mImageView;//用于显示照片
         private File mPhotoFile;
         private String mPhotoPath;
         public final static int CAMERA_RESULT = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                mButton = (Button) findViewById(R.id.button);
                 mButton.setOnClickListener(new ButtonOnClickListener());
                 mImageView = (ImageView) findViewById(R.id.imageView);
             }

                 private class ButtonOnClickListener implements View.OnClickListener {
                 public void onClick(View v) {
                         try {
                                 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//开始拍照
                                mPhotoPath = getSDPath()+"/"+ getPhotoFileName();//设置图片文件路径,getSDPath()和getPhotoFileName()具体实现在下面

                                mPhotoFile = new File(mPhotoPath);
                                if (!mPhotoFile.exists()) {
                                        mPhotoFile.createNewFile();//创建新文件
                                    }
                                 intent.putExtra(MediaStore.EXTRA_OUTPUT,//Intent有了图片的信息
                                                Uri.fromFile(mPhotoFile));
                              startActivityForResult(intent, CAMERA_RESULT);//跳转界面传回拍照所得数据
                            } catch (Exception e) {
                             }
                     }
             }
    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();

    }


                 private String getPhotoFileName() {
                 Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat dateFormat = new SimpleDateFormat(
                                 "'IMG'_yyyyMMdd_HHmmss");
                 return dateFormat.format(date)  +".jpg";
             }

                 @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                 super.onActivityResult(requestCode, resultCode, data);
                 if (requestCode == CAMERA_RESULT) {
                         Bitmap bitmap = BitmapFactory.decodeFile(mPhotoPath, null);
                         mImageView.setImageBitmap(bitmap);
                   }
    }
    }


下面对获取SD卡路径进行补充,有两种情况,我用了第二种

用第二种需要加一个包

import android.os.Environment;


获取sd卡路径 
方法一: private String folder = "/sdcard/DCIM/Camera/"(SD卡上拍照程序的图片存储路径); //写死绝对路径,不赞成使用

方法二: 
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(); 
       
}

然后:在后面加上斜杠,在加上文件名 
String fileName = getSDPath() +"/" + name;//以name存在目录中


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页