主界面
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);
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);
if( requestCode==111&&resultCode== Activity.RESULT_OK){
Toast.makeText(this, "对", Toast.LENGTH_SHORT).show();
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() {
View decorView = getWindow().getDecorView();
decorView.setDrawingCacheEnabled(true);
decorView.buildDrawingCache();;
Bitmap drawingCache = decorView.getDrawingCache();
imageView.setImageBitmap(drawingCache);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String format = sdf.format(new Date());
Date date = new Date();
String filePath="/mnt/sdcard/Pictures/IMG_"+format+".jpg";
try {
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>