5.26

所花时间(包括上课):1

打码量(行):100

博客量(篇):1

了解到知识点:学习调用手机相机

 

 package com.example.myapp;

 

import android.content.Intent;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

 

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private static final int REQUEST_IMAGE_CAPTURE = 1;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

    public void dispatchTakePictureIntent(View view) {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

        }

    }

 

    @Override

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            // 获取从相机返回的照片数据

            Bundle extras = data.getExtras();

            // 可以将照片显示在 ImageView 中或者进行其他处理

        }

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <Button

        android:id="@+id/takePictureButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Take Picture"

        android:onClick="dispatchTakePictureIntent"

        android:layout_centerInParent="true" />

 

</RelativeLayout>

posted @ 2024-05-26 17:11  赵千万  阅读(1)  评论(0编辑  收藏  举报