第十课 各种调用本地应用

主界面

package com.example.day10;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private VideoView vv;
    private Uri uri;

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

        inti();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if( requestCode==102&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "权限获取成功。", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "权限获取失败。", Toast.LENGTH_SHORT).show();
        }
    }

    private void inti() {
        imageView=findViewById(R.id.main_image);
        vv=findViewById(R.id.main_vieoView);
        //动态获取权限
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, 102);
        }
    }

    public void click(View view) {
        switch (view.getId()) {
                //打电话
            case R.id.main_button_phone:
                call_phone();
                break;
                //照相机
            case R.id.main_button_camera:
                to_camera();
                break;
                //摄像机
            case R.id.main_button_video:
                video();
                break;
                //截图
            case R.id.main_button_screenshort:
                screenShort();
                break;
                //浏览器
            case R.id.main_button_internet:
                openInternet();
                break;
        }
    }

    private void to_camera() {
        Intent it = new Intent();
        it.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//        String createname=createname();
//        new File( "");
        uri=FileProvider.getUriForFile(this,
                "com.example.day10.fileProvider",
                new File("/sdcard/DCIM/Camera/aaa.jpg")
                );
        it.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        startActivityForResult(it,222);
    }

    private void video() {
        Intent it = new Intent();
        it.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(it,111);//设置请求吗
    }
    //获取界面跳转的返回值


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //requestCode 请求吗  resultCode结果码,用户点击对勾就是成功,点击x就是失败
        if( requestCode==111&&resultCode== Activity.RESULT_OK){
            Toast.makeText(this, "对", Toast.LENGTH_SHORT).show();
            //获取视频的数据方法哦videoView中
            Uri data1 = data.getData();
            vv.setVideoURI(data1);
            vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    vv.start();
                }
            });
            vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    vv.start();
                }
            });
        }else if( requestCode==222&&resultCode==Activity.RESULT_OK){
            imageView.setImageURI(uri);
        }
        else{
            Toast.makeText(this, "错。", Toast.LENGTH_SHORT).show();
        }
    }

    private void screenShort() {
        // TODO 1:拿到窗口View
        View decorView = getWindow().getDecorView();
        //TODO 2:设置能缓存 并缓存一下
        decorView.setDrawingCacheEnabled(true);//设置能缓存
        decorView.buildDrawingCache();;//缓存
        //TODO 3:获得缓存图片
        Bitmap drawingCache = decorView.getDrawingCache();
        imageView.setImageBitmap(drawingCache);
        //TODO 4:存储到SD卡。
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String format = sdf.format(new Date());


        Date date = new Date();
        //date.getYear()+date.getMonth()+date.getDay()+"-"+date.getHours()+date.getMinutes()+date.getSeconds()
        String filePath="/mnt/sdcard/Pictures/IMG_"+format+".jpg";
        try {
            //图片的格式   压缩质量0-100             文件流  位置
            drawingCache.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(filePath));
            //设置在相册中可见
            MediaStore.Images.Media.insertImage(getContentResolver(),drawingCache,"标题","描述");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    //打开浏览器,并跳转到指定网页。
    private void openInternet() {
        Intent it = new Intent();
        it.setAction(Intent.ACTION_VIEW);
        it.setData(Uri.parse("https://bz.zzzmh.cn/"));
        startActivity(it);
    }

    //打电话
    private void call_phone() {
        Intent it = new Intent();
        it.setAction(Intent.ACTION_CALL);
        it.setData(Uri.parse("tel:"+"123321"));
        startActivity(it);
    }
}

清单列表

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.day10">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_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"
        tools:ignore="GoogleAppIndexingWarning">
        <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:authorities="com.example.day10.fileProvider"
            android:name="androidx.core.content.FileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths"/>
    </provider>
    </application>

</manifest>

对应的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="打电话"
                android:id="@+id/main_button_phone"
                android:onClick="click"
                />
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="照相机"
                android:id="@+id/main_button_camera"
                android:onClick="click"
                />
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="摄像机"
                android:id="@+id/main_button_video"
                android:onClick="click"
                />
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="浏览器"
                android:id="@+id/main_button_internet"
                android:onClick="click"
                />
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="截图"
                android:id="@+id/main_button_screenshort"
                android:onClick="click"
                />
        </LinearLayout>
        <ImageView
            android:layout_width="200dp"
            android:layout_height="350dp"
            android:id="@+id/main_image"
            />
        <VideoView
            android:layout_width="200dp"
            android:layout_height="350dp"
            android:id="@+id/main_vieoView"
            />
    </LinearLayout>


</ScrollView>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值