权限
<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"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>
动态权限
requestPermissions(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.READ_SMS,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.READ_CALL_LOG
},101);
<Button
android:text="打开浏览器"
android:onClick="openBrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="拨打电话"
android:onClick="dissuade"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="截图"
android:onClick="chapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="打开相册"
android:onClick="openPho"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="打开摄像机"
android:onClick="openCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
public void openBrow(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("https://www.baidu.com/s?ie=utf-8&wd=%E7%99%BE%E5%BA%A6&tn=62076830_13_hao_pg");
intent.setData(uri);
startActivity(intent);
}
//拨打电话
public void dissuade(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri uri = Uri.parse("tel:"+"17861721709");
intent.setData(uri);
startActivity(intent);
}
//截图
public void chapter(View view) {
View decorView = getWindow().getDecorView();
//设置缓存
decorView.setDrawingCacheEnabled(true);
//截图
decorView.buildDrawingCache();
//获取截图
Bitmap bitmap = decorView.getDrawingCache();
img.setImageBitmap(bitmap);
try {
bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream("/sdcard/DCIM/ji.png"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
//打开相册
public void openPho(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,101);
}
//打开摄像机
public void openCamera(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,202);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==101&&resultCode==RESULT_OK){
Uri uri = data.getData();
img.setImageURI(uri);
}
if (requestCode==202&&resultCode==RESULT_OK){
Uri uri = data.getData();
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.start();
}
});
}
if (requestCode==404&&resultCode==RESULT_OK){
img.setImageURI(uri);
}
}