实现功能:照相+录像+浏览器+打电话+截屏
清单文件中添加权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><!--打电话权限-->
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
照相图片的存储需要在清单文件注册一个provider
但是需要先在res中创建一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--external-path SD卡的根目录 name自定义 path路径-->
<external-path
name="external_storage_root"
path=".">
</external-path>
</paths>
然后清单文件中添加
· name :FileProvider全类名。
· authorities:配置一个 FileProvider 的名字,它在当前系统内需要是唯一值。
· exported:表示该 FileProvider 是否需要公开出去,这里不需要,所以是 false。
· granUriPermissions:是否允许授权文件的临时访问权限。这里需要,所以是 true。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.exam.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/path" />
</provider>
最后,代码部分.
activity布局
<?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"
android:orientation="vertical"
tools:context=".day010.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开浏览器"
android:onClick="openChrome"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打电话"
android:onClick="callphone"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="截图"
android:onClick="reatemImage"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="照相"
android:onClick="camera"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="录像"
android:onClick="Video"
/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity代码
package com.example.exam.day010;
import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.VideoView;
import com.example.exam.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private ImageView image;
private VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA},101);
}
image = (ImageView) findViewById(R.id.image);
vv = (VideoView) findViewById(R.id.vv);
}
//ACTION_VIEW
public void openChrome(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri=Uri.parse("https://www.baidu.com");
intent.setData(uri);
startActivity(intent);
}
public void callphone(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"10086"));
startActivity(intent);
}
public void reatemImage(View view) {
View view1 = getWindow().getDecorView();
view1.setDrawingCacheEnabled(true);
Bitmap bitmap = view1.getDrawingCache();
image.setImageBitmap(bitmap);
try {
bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/aa.png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* sd卡中的路径要提供给相机
* 1. sd提供 --注册(URI)
* 2. 相机 索要方
* @param view
*/
public void camera(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/test.png");
Uri uri = FileProvider.getUriForFile(this,"com.example.exam.fileProvider",file);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
image.setImageURI(uri);
startActivity(intent);
}
public void Video(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,101);
}
@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();
vv.setVideoURI(uri);//告诉videoview播放哪个视频
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vv.start();
}
});
}
}
}