本文主要是介绍照相机与fileprovider机制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
照相机与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() {@Overridepublic 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() {@Overridepublic 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{@Overridepublic 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)){//查找结束显示listViewMyAdapter 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{@Overridepublic int getCount() {return devices.size();}@Overridepublic Object getItem(int i) {return devices.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic 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-pathname="external_storage_root"path="."></external-path></paths>
清单文件
<providerandroid:authorities="com.example.day10_camera"android:name="androidx.core.content.FileProvider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths"/></provider>
这篇关于照相机与fileprovider机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!