添加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
权限判断
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE
}, 100);
}
打开摄像机
btVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(intent);
}
});
打开浏览器
btBrowser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com/?tn=62095104_19_oem_dg"));
startActivity(intent);
}
});
打电话
btCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "15147550291"));
startActivity(intent);
}
});
截图
btJiietu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View decorView = getWindow().getDecorView();
decorView.setDrawingCacheEnabled(true);
decorView.buildDrawingCache();
Bitmap bitmap = decorView.getDrawingCache();
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/DCIM/" + "aaa.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
iv.setImageBitmap(bitmap);
}
});
播放视频
btPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vv.setVideoPath("/sdcard/DCIM/犯罪都市-马东锡-高清(2).mp4");
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vv.start();
}
});
}
});
**打开图库
btTuku.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 101);
}
});
**打开相机更换头像
private String path = "/sdcard/DCIM/aaa.jpg";
btCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriForFile = FileProvider.getUriForFile(MainActivity.this, "com.example.myday0723", new File(path));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
startActivityForResult(intent, 102);
}
});
}
判断请求码
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Glide.with(this).load(uri).circleCrop().into(iv);
} else if (requestCode == 102 && resultCode == Activity.RESULT_OK) {
Glide.with(this).load(path).circleCrop().into(iv);
}
}
写一个XML文件
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="mypath"
path="DCIM"></external-path>
</paths>
清单文件
<!--
设置uri权限
app包名
FileProvider路径
-->
<!--
看FileProvider源码
xml路径
-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myday0723"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths">
</meta-data>
</provider>
布局
<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">
<Button
android:id="@+id/bt_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打电话"></Button>
<Button
android:id="@+id/bt_browser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开浏览器">
</Button>
<Button
android:id="@+id/bt_jiietu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="截图"></Button>
<Button
android:id="@+id/bt_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开摄像机录制视频">
</Button>
<Button
android:id="@+id/bt_play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放录制的视频"></Button>
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal">
</VideoView>
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher_round">
</ImageView>
<Button
android:id="@+id/bt_tuku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开图库更换头像">
</Button>
<Button
android:id="@+id/bt_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开相机更换头像"></Button>
</LinearLayout>