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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在HTML5中,可以利用JavaScript调用Android设备的相机相册。要实现这个功能,首先需要使用`<input>`标签来创建一个文件上传输入框,然后通过JavaScript来触发文件上传的事件。 ``` <input type="file" id="file-input" accept="image/*" capture> ``` 在上述代码中,`accept`属性用于指定上传的文件类型为图片,而`capture`属性表示可以通过设备的相机来拍照。 然后,在JavaScript中添加以下代码,当点击上传输入框时,弹出相机相册选择: ``` document.getElementById("file-input").addEventListener("click", function() { if (navigator.camera) { // 检测是否支持Camera API navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.PHOTOLIBRARY // 选择相册 }); } else { alert("此设备不支持相机功能"); } }); function onSuccess(imageURI) { // 在成功获取图片后的处理逻辑 } function onFail(message) { // 获取图片失败时的处理逻辑 alert('获取图片失败: ' + message); } ``` 以上代码中,我们通过`navigator.camera.getPicture`方法来调用Android设备的相机相册,可以设置参数来控制图片的质量、目标类型和来源类型。`onSuccess`函数用于在成功获取图片时进行处理,而`onFail`函数则用于在获取图片失败时进行处理。 这样,当用户点击上传输入框时,会弹出相机相册的选择,用户可以拍照或从相册中选择图片进行上传。 ### 回答2: 要在H5中调用Android的弹框相机相册,可以通过以下步骤实现: 1. 在H5页面中,使用Javascript监听触发事件,例如点击一个按钮。 2. 在触发事件的回调函数中,通过Javascript与Android端进行通调用Android相机相册功能。可以使用WebView的addJavascriptInterface()方法创建一个Java对象,用于Javascript与Android端的交互。 3. 在Java对象中,实现调用Android相机相册的代码逻辑。可以通过Intent启动Android相机相册应用,并在回调中获取所选图片的路径或相机拍摄的照片数据。 4. 接收到图片路径或数据后,可以通过Javascript回调函数将获取的图片展示在H5页面上。 需要注意的是,调用Android相机相册需要申请相应权限,例如相机权限和访问外部存储器权限。同时,还需要处理Android不同版本的兼容性问题,如Android 6.0以上需要动态申请权限。另外,还需要注意Android设备的厂商定制和系统版本的差异可能导致一些兼容性问题。 总之,通过以上步骤,可以在H5页面中调用Android的弹框相机相册实现拍照和选择图片的功能。 ### 回答3: 在H5页面中可以通过调用Android相机相册实现弹框拍照和选择照片的功能。具体的实现步骤如下: 1. 首先,在H5页面中添加一个按钮或者其他的触发元素,用于触发调用相机相册功能。 2. 在按钮的点击事件中,使用JavaScript与Android的交互方法,例如使用Webview的`addJavascriptInterface`方法,将一个JavaScript对象注入到Android中。 3. 在注入的JavaScript对象中,定义一个方法,用于调用Android相机相册。可以通过Android的`Intent`来实现调用相机的代码如下: ```java private static final int REQUEST_CAMERA = 1; public void openCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); } ``` 调用相册的代码如下: ```java private static final int REQUEST_GALLERY = 2; public void openGallery() { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_GALLERY); } ``` 4. 在Android中,通过`onActivityResult`方法来处理相机相册的返回结果。可以在其中获取到用户拍摄的照片或选择的照片的Uri。 5. 将获取到的照片Uri返回给H5页面,可以通过调用JavaScript的方法,在Android中使用`WebView`的`loadUrl`方法执行JavaScript代码,来实现将照片Uri传递给H5页面。 通过以上步骤,H5页面就可以调用Android相机相册,并将拍摄的照片或选择的照片显示在页面上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值