android中调用相机拍照返回null的问题的终极解决办法
Posted 客舍青
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中调用相机拍照返回null的问题的终极解决办法相关的知识,希望对你有一定的参考价值。
private ArrayList<String> mImageList; private static final int REQUEST_CAMERA = 5; // 相机拍照标记 private static final int CAMERA_REQUEST_CODE = 1; /** * 拍照获取照片 */ protected void selectPicFromCamera() { if (!SDCardUtils.isSDCardEnable()){ //不存在sd卡 Toast.makeText(this,R.string.sd_disable,Toast.LENGTH_SHORT).show(); return; } //android 6.0之后需要动态申请权限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE); } Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(this.getPackageManager()) != null) { tempFile = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg"); LogUtils.e(tempFile.getAbsolutePath()); FileUtils.createFile(tempFile); Uri uri = FileProvider.getUriForFile(this, FileUtils.getApplicationId(this) + ".provider", tempFile); List<ResolveInfo> resInfoList = this.getPackageManager() .queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); //Uri.fromFile(tempFile) startActivityForResult(cameraIntent, REQUEST_CAMERA); } else { Toast.makeText(this, "打开相机失败", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK){ if (tempFile != null){ mImageList.add(tempFile.getAbsolutePath()); } } }
以上是关于android中调用相机拍照返回null的问题的终极解决办法的主要内容,如果未能解决你的问题,请参考以下文章
Android7.0调用系统相机拍照读取系统相册照片+CropImageView剪裁照片