照相机与fileprovider机制
作者:互联网
照相机与fileprovider机制
清单文件
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Activity动态添加权限
private void init() {
String[] strings = {
Manifest.permission.INTERNET,
Manifest.permission.CALL_PHONE,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(strings,100);
}
}
浏览器
//浏览器
public void openbrowser(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
拨号
//拨号
public void opencall(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"10086"));
startActivity(intent);
}
相机
//调用系统相机
public void opencamera(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//战利品
File file = new File(Environment.getExternalStorageDirectory() + “/test_image.png”);//存到这个位置
Uri uri = FileProvider.getUriForFile(this,“com.example.day10_camera”,file);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
//显示控件
startActivity(intent);
}
摄像
//调用系统视频
public void openvideo(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();
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.start();
}
});
}
}
截图
//截图
public void jietu(View view) {
View decorView = getWindow().getDecorView();//顶层视图
decorView.setDrawingCacheEnabled(true);
Bitmap bitmap = decorView.getDrawingCache();//获取图片
try {
//存SD卡 1类型 2质量 3地址
bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory()+"/aaaa.png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
蓝牙
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);//查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//结束查找
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//绑定
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver,filter);
init();
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
adapter = manager.getAdapter();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = devices.get(i);
device.createBond();//配对
}
});
//打开蓝牙
public void openblue(View view) {
Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(intent);
}
//搜索蓝牙
public void search(View view) {
adapter.startDiscovery();
}
//关闭蓝牙
public void close(View view) {
adapter.disable();
}
//广播
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
//找到的蓝牙
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
Log.i(TAG, "onReceive: 搜索到的蓝牙"+name);
if (name != null){
//存集合
devices.add(device);
}
}else if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
//查找结束显示listView
MyAdapter myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);
}else if (intent.getAction().equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int bondState = device.getBondState();
if (bondState == device.BOND_NONE){
Log.i(TAG, "onReceive: 未绑定");
}else if (bondState == device.BOND_BONDING){
Log.i(TAG, "onReceive: 绑定中");
}else if (bondState == device.BOND_BONDED){
Log.i(TAG, "onReceive: 已绑定");
}
}
}
}
//适配器
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return devices.size();
}
@Override
public Object getItem(int i) {
return devices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null){
holder = new ViewHolder();
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null);
holder.nameId = view.findViewById(R.id.bluename_id);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.nameId.setText(devices.get(i).getName());
return view;
}
class ViewHolder{
TextView nameId;
}
}
FileProvider文件共享
//FileProvider文件共享
//SD卡的路径要提供给相机
//SD 提供 --- 注册
//相机 索要方
Uri uri = FileProvider.getUriForFile(this,"com.example.day10_camera",file);
xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
ecternal-path:SD卡的根目录 name自定义 path image/.根目录
<external-path
name="external_storage_root"
path="."></external-path>
</paths>
清单文件
<provider
android:authorities="com.example.day10_camera"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
标签:void,fileprovider,intent,new,Intent,机制,public,照相机,view 来源: https://blog.csdn.net/SingleTigerkin/article/details/99468349