打电话 打开浏览器 查看图库 摄像机 截图 拍照

各种权限 打电话 打开浏览器 查看图库 摄像机 截图 拍照

**首先加入权限 并且获取动态权限 **

 <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

动态获取

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    "android.permission.CAMERA",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.CALL_PHONE"
            },110);

        }

1.打电话

				  //打电话
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+"13*********"));//数据电话
                startActivity(intent);
			 

2.打开浏览器

				 Intent intent1 = new Intent();
                intent1.setAction(Intent.ACTION_VIEW);//浏览器action
                intent1.setData(Uri.parse("https://www.baidu.com"));//数据:网址
                startActivity(intent1);
			 

3.查看图库

			 Intent intent2 = new Intent();
                intent2.setAction(Intent.ACTION_PICK);//图库action  PICK
                intent2.setType("image/*");//打开图库照片
                startActivityForResult(intent2,105);

4.摄像机


			 	Intent intent3 = new Intent();
                intent3.setAction(MediaStore.ACTION_VIDEO_CAPTURE);//摄像机action
                startActivityForResult(intent3,106);

5.截图

  //获得窗体
        View view1 = getWindow().getDecorView();
        //设置可以缓存
        view1.setDrawingCacheEnabled(true);
        //缓存一下
        view1.buildDrawingCache();
        //获得缓存的图片
        Bitmap bitmap = view1.getDrawingCache();

        img.setImageBitmap(bitmap);

        try {
            //参数一 格式  参数二 质量 0-100  参数三 输出流
            //Bitmap.CompressFormat format, int quality, OutputStream stream
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream("/sdcard/aaa.jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

6.照相机
1.xml文件中 创建paths文件

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

    <external-path
        name="mypath"
        path="DCIM"></external-path>

</paths>

2.清单文件

 <provider
            android:grantUriPermissions="true"
            android:authorities="com.example.day10_quanxian_work01"
            android:name="androidx.core.content.FileProvider">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths"></meta-data>

        </provider>

3.打开相机拍照代码

  Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        //设置保存路径
        Uri uri = FileProvider.getUriForFile(this,"com.example.day10_quanxian_work01",new File(path));
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        startActivityForResult(intent,107);

最后根据请求码实现功能


@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //图库
        if (requestCode == 105 && resultCode == Activity.RESULT_OK) {
            Uri tuku_= data.getData();
            img.setImageURI(tuku_);
        }else if(requestCode == 106 && resultCode == Activity.RESULT_OK){//摄像机
            Uri shexiang = data.getData();
            vv.setVideoURI(shexiang);
            vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    vv.start();
                }
            });
        }else if(requestCode == 107 && resultCode == Activity.RESULT_OK){//拍照
           img.setImageBitmap(BitmapFactory.decodeFile(path));
        }
    }

你可能感兴趣的:(android)