Andriod studio 學習 之 照相+錄像+瀏覽器+打電話+截屏

實現功能:照相+錄像+瀏覽器+打電話+截屏

清單文件中添加權限

<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();
                }
            });
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章