zoukankan      html  css  js  c++  java
  • 调取系统的相机(展示图片)

    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;

    import java.io.File;

    public class MainActivity extends AppCompatActivity {

    private Button btn_StartCamera, btn_StartCameraInGallery;
    private ImageView iv_CameraImg;

    private static final String TAG = "main";
    private static final String FILE_PATH = "/sdcard/syscamera.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_StartCamera = (Button) findViewById(R.id.btn_StartCamera);
    btn_StartCameraInGallery = (Button) findViewById(R.id.btn_StartCameraInGallery);
    iv_CameraImg = (ImageView) findViewById(R.id.iv_CameraImg);

    btn_StartCamera.setOnClickListener(click);
    btn_StartCameraInGallery.setOnClickListener(click);
    }
    private View.OnClickListener click = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

    Intent intent = null;
    switch (v.getId()) {
    // 指定相机拍摄照片保存地址
    case R.id.btn_StartCamera:
    intent = new Intent();
    // 指定开启系统相机的Action
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    // 根据文件地址创建文件
    File file = new File(FILE_PATH);
    if (file.exists()) {
    file.delete();
    }
    // 把文件地址转换成Uri格式
    Uri uri = Uri.fromFile(file);
    // 设置系统相机拍摄照片完成后图片文件的存放地址
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, 0);
    break;
    // 不指定相机拍摄照片保存地址
    case R.id.btn_StartCameraInGallery:
    intent = new Intent();
    // 指定开启系统相机的Action
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    startActivityForResult(intent, 1);
    break;
    default:
    break;
    }

    }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "系统相机拍照完成,resultCode="+resultCode);

    if (requestCode == 0) {
    File file = new File(FILE_PATH);
    Uri uri = Uri.fromFile(file);
    iv_CameraImg.setImageURI(uri);
    } else if (requestCode == 1) {
    Log.i(TAG, "默认content地址:"+data.getData());
    iv_CameraImg.setImageURI(data.getData());
    }
    }
    }






    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.18229167" />

    <Button
    android:id="@+id/btn_StartCamera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="第一个"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.242"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.29000002" />

    <Button
    android:id="@+id/btn_StartCameraInGallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/btn_StartCamera"
    app:layout_constraintStart_toEndOf="@+id/btn_StartCamera"
    app:layout_constraintTop_toTopOf="@+id/btn_StartCamera" />

    <ImageView
    android:id="@+id/iv_CameraImg"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn_StartCameraInGallery"
    app:srcCompat="@mipmap/ic_launcher" />

    </android.support.constraint.ConstraintLayout>















  • 相关阅读:
    EOF输入
    2019春总结作业
    2019春第二次课程设计报告
    2019春第一次课程设计实验报告
    2019第一次作业的项目模块结构介绍
    2019春第十二周作业
    2019春第十一周作业
    2019春第十周作业
    2019春第九周作业
    2019春第八周作业
  • 原文地址:https://www.cnblogs.com/ysxy/p/9003039.html
Copyright © 2011-2022 走看看