Android开发 实现调用相机、相册、拨打电话、发送短信功能

Android开发 实现调用相机、相册、拨打电话、发送短信功能

参考一:https://blog.csdn.net/qq_28664681/article/details/106764195
参考二:https://blog.csdn.net/nuannuanloveai/article/details/83786032

实现效果

在这里插入图片描述

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:context=".MainActivity">
    <Button
        android:id="@+id/take_photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拍照"
        android:textSize="20dp" />
    
    <Button
        android:id="@+id/choose_from_album"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/take_photo"
        android:layout_marginTop="21dp"
        android:text="相册"
        android:textSize="20dp" />

    <Button
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/choose_from_album"
        android:layout_marginTop="21dp"
        android:text="拨打电话"
        android:textSize="20dp" />

    <Button
        android:id="@+id/mail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phone"
        android:layout_marginTop="21dp"
        android:text="发送短信"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/choose_from_album"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" />
</RelativeLayout>

在这里插入图片描述

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    tools:context="cn.k23.test9callground.MailActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="电话号码"
        android:inputType="number"
        android:id="@+id/num_btn"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="短信内容"
        android:id="@+id/mes_btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="发送"
        android:textColor="#ff0066"
        android:id="@+id/send_btn"/>
</LinearLayout>

activity_phone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    android:orientation="vertical"
    tools:context="cn.k23.test9callground.PhoneActivity">

    <TextView
        android:layout_width="325dp"
        android:layout_height="30dp"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入电话号码"
        android:inputType="number"
        android:id="@+id/phoneNum"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/call_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="拨号"
            android:textSize="20dp" />
    </RelativeLayout>
</LinearLayout>

albums.xml

<?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/pestDetection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:text="图像检测"
        android:textSize="20dp" />

    <Button
        android:id="@+id/pictureSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="保存图像"
        android:layout_toRightOf="@+id/pestDetection"
        android:layout_marginLeft="80dp"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pictureSave"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>

camera.xml

<?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/pestDetection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:text="图像检测"
        android:textSize="20dp" />

    <Button
        android:id="@+id/pictureSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="保存图像"
        android:layout_toRightOf="@+id/pestDetection"
        android:layout_marginLeft="80dp"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pictureSave"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>

在AndroidManifest.xml中添加注册

在这里插入图片描述

<activity android:name="Albums"></activity>
<activity android:name="Camera"></activity>
<activity android:name="PhoneActivity"></activity>
<activity android:name="MailActivity"></activity>
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.example.lenovo.cameraalbumtest.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_path" />
</provider>

在AndroidManifest.xml中添加权限

在这里插入图片描述

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.CAMERA"/>
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.SEND_SMS" />

添加图片保存的xml路径(新建file_path.xml文件)

在这里插入图片描述

file_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
    name="my_images"
    path="." />
</paths>

如需在真机上测试,在gradle.properties里添加

android.injected.testOnly=false

猜你喜欢

转载自blog.csdn.net/weixin_42436236/article/details/126302148