Android學習之-----fileprovider機制調用系統設置

照相機與fileprovider機制

瀏覽器

撥號

相機

攝像

截圖

藍牙

FileProvider文件共享

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

代碼實現:

package com.example.rikao;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    /**
     * 2.	調用系統相機實施自拍並保存到本地
     * 3.	調用系統攝像機,錄製音視頻並保存到本地
     * 4.	調用系統瀏覽器,完成搜索java
     * 5.	打開藍牙,顯示搜索到附近的藍牙列表
     * 6.	截圖藍牙列表,保存到本地
     * 7.	獲取本機藍牙信息,保存到txt
     * 8.	實現關閉藍牙
     * 9.	實現一鍵撥打10086
     * @param savedInstanceState
     *
     *
     */
    private ArrayList<String> list=new ArrayList<>();
    private ListView lv;
    private BluetoothAdapter adapter;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);

        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        adapter = bluetoothManager.getAdapter();

        MyReceiver receiver=new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(receiver,intentFilter);

        String[] strings=new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.INTERNET,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,};

        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
            requestPermissions(strings,100);
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void click1(View view) {
        Intent intent=new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        File file= new File(Environment.getExternalStorageDirectory()+"/rikao.png");
        Uri uri = FileProvider.getUriForFile(this, "com.example.rikao", file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        startActivity(intent);
    }

    public void click2(View view) {
        Intent intent=new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivity(intent);
    }

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

    public void click4(View view) {
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivity(intent);
        Log.i(TAG, "click4:11111 ");
    }

    public void click5(View view) {
        adapter.startDiscovery();
    }

    public void click6(View view) {
        View view1 = getWindow().getDecorView();
        view1.setDrawingCacheEnabled(true);
        Bitmap bitmap = view1.getDrawingCache();

        try {
            bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory()+"/jietu.png"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

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

    class MyReceiver extends BroadcastReceiver{
        private static final String TAG = "MyReceiver";
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
                Log.i(TAG, "onReceive: 搜索中");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                if (name != null) {
                    list.add(name);
                }
            }else if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                MyAdapter myAdapter = new MyAdapter();
                lv.setAdapter(myAdapter);
            }
        }
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HoldView holdView ;
            if (convertView==null){
                holdView=new HoldView();
                convertView= LayoutInflater.from(MainActivity.this).inflate(R.layout.item,parent,false);
                holdView.name=convertView.findViewById(R.id.tex);
                convertView.setTag(holdView);
            }else {
               holdView= (HoldView) convertView.getTag();
            }
            holdView.name.setText(list.get(position));

            return convertView;
        }

        class HoldView{
            TextView name;
        }
    }

}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tex"
        android:text="sss"
        android:textSize="25sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="click1"
            android:text="調用系統相機"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="click6"
            android:text="截圖"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:onClick="click7"
            android:text="截圖"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>

    <Button
        android:onClick="click2"
        android:text="調用系統攝像機"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="click3"
        android:text="調用系統瀏覽器,完成搜索java"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="click4"
            android:text="打開藍牙"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="click5"
            android:text="顯示搜索到附近的藍牙列表"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>



</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rikao">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:exported="false"
            android:grantUriPermissions="true"
            android:authorities="com.example.rikao"
            android:name="android.support.v4.content.FileProvider">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/rikao"/>
        </provider>

    </application>

</manifest>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章