/** * take pic */ private void toTakePic() { /**android 6.0 权限申请**/ if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_FOR_WRITE_PERMISSION); //判断是否需要 向用户解释,为什么要申请该权限 ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); } else { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { //请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA); //判断是否需要 向用户解释,为什么要申请该权限 ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS); } else { if (Build.VERSION.SDK_INT >= 23) { // Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null); // this.sendBroadcast(i); // long dateTaken = System.currentTimeMillis(); // String name = dateTaken + ".jpg"; // String fileName = name; // ContentValues values = new ContentValues(); // values.put(MediaStore.Images.Media.TITLE, fileName); // values.put("data", fileName); // values.put(MediaStore.Images.Media.PICASA_ID, fileName); // values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName); // values.put(MediaStore.Images.Media.DESCRIPTION, fileName); // values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName); // Uri photoUri = getContentResolver().insert( // MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); mUploadPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) .getAbsolutePath() + File.separator + System.currentTimeMillis() + ".png"; File tempFile = new File(mUploadPhotoPath); Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); inttPhoto.setAction(MediaStore.ACTION_IMAGE_CAPTURE); inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); inttPhoto.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(inttPhoto, REQUEST_TAKE_PICTURE); } else { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // 指定输出地址 mUploadPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) .getAbsolutePath() + File.separator + System.currentTimeMillis() + ".png"; File tempFile = new File(mUploadPhotoPath); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(intent, REQUEST_TAKE_PICTURE); // 通过StartActivityForResult } else { Toast.makeText(GeRenInFoActivity.this, "未找到存储卡", Toast.LENGTH_SHORT).show(); } } } } }//可以在回调中处理事件@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == CODE_FOR_WRITE_PERMISSION) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted } else { // Permission Denied } } }
6.0权限问题
最新推荐文章于 2021-11-10 18:04:16 发布