博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 打开系统相机,
阅读量:5316 次
发布时间:2019-06-14

本文共 7330 字,大约阅读时间需要 24 分钟。

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

2.第二步创建activity_creama.xml

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注册

 第二部:

xml文件添加一个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);        } }}

 

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

你可能感兴趣的文章
An easy problem
查看>>
windows 安装yaml支持和pytest支持等
查看>>
读书笔记:季羡林关于如何做研究学问的心得
查看>>
面向对象的优点
查看>>
[转载]unix环境高级编程备忘:理解保存的设置用户ID,设置用户ID位,有效用户ID,实际用户ID...
查看>>
堆排序
查看>>
套接口和I/O通信
查看>>
thinkpaidE480office安装文件夹
查看>>
eclipse中git插件配置 编辑
查看>>
初识Spark2.0之Spark SQL
查看>>
Atlas Unknown Error
查看>>
变量与常量
查看>>
Dom4J 解析xml ,类查询
查看>>
IIS相关问题及解决方案
查看>>
PAT1070. Mooncake (25)
查看>>
大数据:Parquet文件存储格式
查看>>
知道这20个正则表达式,能让你少写1,000行代码
查看>>
SQL 查询数据库中包含指定字符串的相关表和相关记录
查看>>
(3)Python3笔记之变量与运算符
查看>>
游戏开发Camera之Cinematic Camera-深度
查看>>