android学习之1:拍照保存图片

来源:互联网 发布:大型无人机价格 知乎 编辑:程序博客网 时间:2024/05/16 15:53

android调用物理相机拍照并保存在SD卡上

android调用相机拍照保存在SD卡上主要是使用startActivityForResult和onActivityResult配合使用。使用startActivityForResult开发相机,onactivityResult接受拍照的数据并进行保存。

1.注册权限,即获取保存到SD卡 的权限

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

2.设置打开相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, 1);

3.判断系统是否有SD卡

String sdStatus = Environment.getExternalStorageState();        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用            Log.v("TestFile",                    "SD card is not avaiable/writeable right now.");            return;        }

4.接收拍照返回的bitmap转换成照片,以当前拍照时间为后缀名保存在特定的文件夹内

Bundle bundle = data.getExtras();        Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式        FileOutputStream b = null;        File file = new File("/sdcard/myImage/");        file.mkdirs();// 创建文件夹        Date curDate = new Date(System.currentTimeMillis());//获取当前时间        String str = formatter.format(curDate);        String fileName = "/sdcard/myImage/"+str+".jpg";        try {            b = new FileOutputStream(fileName);            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件        } catch (FileNotFoundException e) {            e.printStackTrace();        } finally {            try {                b.flush();                b.close();            } catch (IOException e) {                e.printStackTrace();            }        }

5.整体代码

package com.yongxingg.carematest;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {    SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd_HHmmss ");    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button) findViewById(R.id.button);    button.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            startActivityForResult(intent, 1);        }    });}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == Activity.RESULT_OK) {        String sdStatus = Environment.getExternalStorageState();        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用            Log.v("TestFile",                    "SD card is not avaiable/writeable right now.");            return;        }        Bundle bundle = data.getExtras();        Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式        FileOutputStream b = null;        File file = new File("/sdcard/myImages/");        file.mkdirs();// 创建文件夹        Date curDate = new Date(System.currentTimeMillis());//获取当前时间        String str = formatter.format(curDate);        String fileName = "/sdcard/myImages/"+str+".jpg";        try {            b = new FileOutputStream(fileName);            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件        } catch (FileNotFoundException e) {            e.printStackTrace();        } finally {            try {                b.flush();                b.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}

}

6.页面布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="点击启动相机" /></LinearLayout>

点击下载

0 0