照相机与fileprovider机制

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

0.权限

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><!--打电话权限-->
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
写SD卡权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
读SD卡权限:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
一.使用隐式意图打开浏览器

//mainActivity

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <Button
        android:text="打开浏览器"
        android:id="@+id/bt_browser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="打电话"
        android:id="@+id/bt_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="打开摄像机"
        android:id="@+id/bt_video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="打开照相机"
        android:id="@+id/bt_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="截屏"
        android:id="@+id/bt_jie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
    <VideoView
        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

 //java文件

String[] strings = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA};
    public static final int REQUEST_CODE = 101;
    public static final int VIDEO_CODE = 102;
    public static final int IMAGE_CODE = 103;
    private Button bt_browser;
    private Button bt_call;
    private Button bt_video;
    private Button bt_camera;
    private Button bt_jie;
    private ImageView iv;
    private VideoView vv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (ActivityCompat.checkSelfPermission(this, strings[0]) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(strings, REQUEST_CODE);
            }
        }

        initView();

    }

    private void initView() {
        bt_browser = (Button) findViewById(R.id.bt_browser);
        bt_call = (Button) findViewById(R.id.bt_call);
        bt_video = (Button) findViewById(R.id.bt_video);
        bt_camera = (Button) findViewById(R.id.bt_camera);
        bt_jie = (Button) findViewById(R.id.bt_jie);
        iv = (ImageView) findViewById(R.id.iv);
        vv = (VideoView) findViewById(R.id.vv);

        bt_browser.setOnClickListener(this);
        bt_call.setOnClickListener(this);
        bt_video.setOnClickListener(this);
        bt_camera.setOnClickListener(this);
        bt_jie.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_browser:
                browser();
                break;
            case R.id.bt_call:
                call();
                break;
            case R.id.bt_video:
                video();
                break;
            case R.id.bt_camera:
                camera();
                break;
            case R.id.bt_jie:
                jie();
                break;
        }
    }


    private void jie() {

        View view = getWindow().getDecorView();

        view.setDrawingCacheEnabled(true);

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();
        iv.setImageBitmap(bitmap);



        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream("/sdcard/a11.png"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void call() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + "18360231761"));
        startActivity(intent);
    }

    private void browser() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);//设置频道
        intent.setData(Uri.parse("https://www.baidu.com"));//设置数据 :网址
        startActivity(intent);
    }

    private void video() {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(intent, VIDEO_CODE);
    }

    Uri uri;

    private void camera() {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

        uri = FileProvider.getUriForFile(this, "com.bawei.1705", new File("/sdcard/MUSIC/demo.png"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, IMAGE_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == VIDEO_CODE && resultCode == Activity.RESULT_OK) {

            Uri uri = data.getData();
            Log.d("ytx", "uri: " + uri);
            vv.setVideoURI(uri);
            vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    vv.start();
                }
            });

        } else if (requestCode == IMAGE_CODE && resultCode == Activity.RESULT_OK) {//请求码一致 结果码正确
            //获得图片的数据
//            Uri uri = data.getData();
//            Log.d("ytx", "uri: "+uri);
            iv.setImageURI(uri);
        }
    }
}

//效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值