实现功能:相机+摄像+浏览器+打电话+截屏
权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
xml布局
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="照相机"
android:id="@+id/camera"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="摄像机"
android:id="@+id/vidicon"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开浏览器"
android:id="@+id/open"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打电话"
android:id="@+id/phone"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="截图"
android:id="@+id/screenshot"></Button>
//存放视频的VideoView
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/videoView"></VideoView>
//存放截图的imageview
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageview"
android:visibility="gone"></ImageView>
</LinearLayout>
MainActivity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button camera;
private Button vidicon;
private Button open;
private Button phone;
private Button screenshot;
private ImageView imageview;
private VideoView videoView;
public static final int VIDEO_CODE=101;
public static final int IMAGE_CODE=102;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = (Button) findViewById(R.id.camera);
vidicon = (Button) findViewById(R.id.vidicon);
open = (Button) findViewById(R.id.open);
phone = (Button) findViewById(R.id.phone);
screenshot = (Button) findViewById(R.id.screenshot);
imageview = (ImageView) findViewById(R.id.imageview);
videoView = (VideoView) findViewById(R.id.videoView);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE,Manifest.permission.CAMERA},100);
}
camera.setOnClickListener(this);
vidicon.setOnClickListener(this);
open.setOnClickListener(this);
phone.setOnClickListener(this);
screenshot.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.camera:
camera();
break;
case R.id.vidicon:
vidicon();
break;
case R.id.open:
open();
break;
case R.id.phone:
phone();
break;
case R.id.screenshot:
screenshot();
break;
}
}
//截图
private void screenshot() {
imageview.setVisibility(View.VISIBLE);
//TODO 1:获得窗口view
View decorView = getWindow().getDecorView();
//TODO 2:设置能缓存,并缓存一下
decorView.setDrawingCacheEnabled(true);///设置能缓存
decorView.buildDrawingCache();//缓存一下
//TODO 3:获得图片缓存
Bitmap bitmap = decorView.getDrawingCache();
imageview.setImageBitmap(bitmap);
//TODO 4:存储到SD卡
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd_HH:mm:ss");
//获取当前系统时间
Date date = new Date();
String format = simpleDateFormat.format(date);
//设置在相册中可见
String filePath = "/sdcard/DCIM/Camera/"+format+".jpg";
try {
//参数一 图片的格式 参数二 压缩质量 0-100 参数三 文件输出流 位置
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//打电话:隐式意图
private void phone() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("tel:"+"13723227887"));
startActivity(intent);
}
//打开浏览器
private void open() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));//网址
startActivity(intent);
}
//摄像机:隐式意图 回调结果
private void vidicon() {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);//设置摄像机频道
startActivityForResult(intent,VIDEO_CODE);//设置请求码
}
Uri uri;
//照相机
private void camera() {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置照相机频道
// 使用fileProvider自己保存
uri = FileProvider.getUriForFile(this,"com.example.day010",new File("/sdcard/DCIM/Camera/aaaa.jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//设置拍摄过成功的输出路径
startActivityForResult(intent,IMAGE_CODE);//设置请求码
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//requestCode 请求码 resultCode 结果码,当用户点击对勾就是成功,点击×就是失败 data 数据
if (requestCode==VIDEO_CODE&& resultCode== Activity.RESULT_OK){
Toast.makeText(this, "对", Toast.LENGTH_SHORT).show();
//获得视频的数据放到videoView中播放
Uri data1 = data.getData();
videoView.setVideoURI(data1);
//播放完成的话循环播放
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoView.start();
}
});
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {//准备完成
videoView.start();//播放
}
});
}else if (requestCode==IMAGE_CODE&&resultCode==Activity.RESULT_OK){
imageview.setImageURI(uri);
}
}
}
动态注册provider
<provider
android:authorities="com.example.day010"
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"></meta-data>
</provider>
存储
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="mypath"
path="DCIM"></external-path>
<external-path
name="mypath2"
path="Movies"></external-path>
</paths>