Andriod studio 学习 之 照相+录像+浏览器+打电话+截屏

本文详细介绍如何在Android应用中实现照相、录像、浏览器、打电话及截屏等功能,包括必要的权限申请、FileProvider配置及具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现功能:照相+录像+浏览器+打电话+截屏

清单文件中添加权限

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><!--打电话权限-->
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

照相图片的存储需要在清单文件注册一个provider
但是需要先在res中创建一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--external-path SD卡的根目录  name自定义  path路径-->
    <external-path
        name="external_storage_root"
        path=".">
    </external-path>
</paths>

然后清单文件中添加
· name :FileProvider全类名。
· authorities:配置一个 FileProvider 的名字,它在当前系统内需要是唯一值。
· exported:表示该 FileProvider 是否需要公开出去,这里不需要,所以是 false。
· granUriPermissions:是否允许授权文件的临时访问权限。这里需要,所以是 true。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.exam.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/path" />
        </provider>

最后,代码部分.
activity布局

<?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"
    tools:context=".day010.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开浏览器"
        android:onClick="openChrome"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打电话"
        android:onClick="callphone"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="截图"
        android:onClick="reatemImage"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="照相"
        android:onClick="camera"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="录像"
        android:onClick="Video"
        />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <VideoView
        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

activity代码

package com.example.exam.day010;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.VideoView;

import com.example.exam.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {
    private ImageView image;
    private VideoView vv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA},101);
        }

        image = (ImageView) findViewById(R.id.image);

        vv = (VideoView) findViewById(R.id.vv);


    }
    //ACTION_VIEW
    public void openChrome(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri=Uri.parse("https://www.baidu.com");
        intent.setData(uri);
        startActivity(intent);
    }

    public void callphone(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+"10086"));
        startActivity(intent);
    }

    public void reatemImage(View view) {
        View view1 = getWindow().getDecorView();
        view1.setDrawingCacheEnabled(true);
        Bitmap bitmap = view1.getDrawingCache();
        image.setImageBitmap(bitmap);
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/aa.png"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * sd卡中的路径要提供给相机
     * 1. sd提供  --注册(URI)
     * 2. 相机 索要方
     * @param view
     */

    public void camera(View view) {

        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/test.png");
        Uri uri = FileProvider.getUriForFile(this,"com.example.exam.fileProvider",file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        image.setImageURI(uri);
        startActivity(intent);
    }

    public void Video(View view) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(intent,101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101&&resultCode==RESULT_OK) {
            Uri uri=data.getData();
            vv.setVideoURI(uri);//告诉videoview播放哪个视频
            vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    vv.start();
                }
            });
        }
    }
}
### 如何在 Android Studio 中集成机智云 SDK #### 准备工作 为了顺利地在 Android Studio 集成机智云 SDK,需先完成如下准备工作: - 注册并登录到机智云开发者平台账户[^3]。 - 安装配置好 Android Studio 开发环境。 #### 添加依赖项 要在项目中引入机智云 SDK,需要编辑 `build.gradle` 文件,在项目的根目录下的 `build.gradle` 文件中的 dependencies 节点加入以下内容来添加 JitPack 仓库支持: ```gradle allprojects { repositories { ... maven { url 'https://jitpack.io' } } } ``` 接着,在模块级别的 `build.gradle` 文件里增加对于具体版本号的 SDK 的依赖声明。例如: ```gradle dependencies { implementation 'com.github.Gizwits-GizKit:gizwifisdk:V2.7.0' } ``` 以上操作完成后同步 Gradle 即可加载所需库文件[^1]。 #### 初始化 SDK 当所有设置都准备好之后就可以初始化 SDK 实例了。通常是在应用程序启动时执行这一过程,比如可以在 Application 类里面做这样的处理: ```java public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // Initialize GizWits SDK with your App ID and Secret Key. String appId = "YOUR_APP_ID"; String appSecret = "YOUR_APP_SECRET"; GAgent.init(this, appId, appSecret); } } ``` 记得替换掉 `"YOUR_APP_ID"` 和 `"YOUR_APP_SECRET"` 成为自己应用对应的值[^2]。 #### 连接设备与控制功能实现 通过调用相应 API 方法可以轻松连接管理 IoT 设备,并获取其状态信息或发送指令改变当前的工作模式等。下面给出一段简单的代码片段用于展示如何发现附近 WiFi 下已绑定至该用户的智能产品实例列表: ```java // Get the list of devices associated with this user account under a specific Wi-Fi network. GAgent.getDeviceList(new IGetDeviceListCallback() { @Override public void onSuccess(List<DeviceInfo> deviceInfoList) { Log.d("TAG", "Found " + deviceInfoList.size() + " devices."); for (DeviceInfo info : deviceInfoList){ Log.d("TAG", "Device Name:" + info.getName()); } } @Override public void onFailure(int code, String msg) { Log.e("TAG", "Failed to get device list. Error Code:" + code + ", Message:" + msg); } }); ``` 这段代码展示了查询关联设备的方法;而针对特定型号产品的属性读写则可通过查阅官方文档找到更详细的说明[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值