Android从相机捕获图像返回空数据

时间:2018-11-02 06:29:13

标签: android permissions android-camera-intent

每次在android应用意图中捕获图像时,返回null。请在下面查看我正在使用的代码。我尝试了多种方法来获取许可和意图集,但仍然无法获取数据。

相机动作

 private void takePhotoFromCamera() {

    if(ActivityCompat.checkSelfPermission(ObservationsView.this, 
    Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(getExternalCacheDir(),
                String.valueOf(System.currentTimeMillis()) + ".jpg");fileUri = Uri.fromFile(file);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }else {
    String[] permissionRequest = {Manifest.permission.CAMERA};
    requestPermissions(permissionRequest, 8675309);
    checkPermission();
    }
}

这是响应代码

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK ) {
        Uri selectedImg = null;
        Bitmap bmp = null;
        Bundle extras;
        if (requestCode == CAMERA_REQUEST) {
            selectedImg = data.getData();
            extras = data.getExtras();

            bmp = (Bitmap) extras.get("data");

        }

        else if (requestCode == CAMERA_REQUEST) {

            selectedImg = fileUri;


            // Bitmap mphoto = (Bitmap) data.getExtras().get("data");
            try {
                bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
                if (bmp != null) {
                    bmp = getBitmapFromUri(fileUri);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }

            Uri tempUri = getImageUri(getApplicationContext(), bmp);


            Intent passPhoto = new Intent(this, Photo.class);
            passPhoto.putExtra("image",tempUri);

            passPhoto.putExtra("Caller", getIntent().getComponent().getClassName());
            startActivity(passPhoto);


        }

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码在所有API中获取图像-

 public void takePicture() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = FileProvider.getUriForFile(mContext,
                    BuildConfig.APPLICATION_ID + ".provider",
                    getOutputMediaFile());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
            startActivityForResult(intent, TAKE_IMAGE_REQUEST);
        }

 private File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), getString(R.string.app_folder_name));

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }


        return new File(mediaStorageDir.getPath() + File.separator +
                PROFILE_PIC + getString(R.string.pic_type));
    }

在onActivityResult上-

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_IMAGE_REQUEST:
                if (resultCode == RESULT_OK) {
                    imageView.setImageURI(file);
                }
                break;

            default:
                break;

        }
    }

此外,您需要在Manifest.xml文件中定义Provider-

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

最后,provider_paths.xml将如下所示-

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

如果您需要任何澄清,请告诉我。