Intent.getData()在Android API 23上返回null

时间:2016-07-26 09:38:43

标签: android android-intent android-6.0-marshmallow

Intent.getData()在Android API 23上返回null并在之前的版本中正常运行?

private void takePicture() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, 0);
}

...

private void choosePicture()
{
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

...

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ...
    Uri selectedImage = data.getData();
    String imagePath = getPath(selectedImage);
    dest = new File(imagePath);
    decodeFile(imagePath);
    ...
}

2 个答案:

答案 0 :(得分:0)

public boolean ifPermissionReady(){
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
            System.out.println("req camera permission*****");
            checkPermission(Manifest.permission.CAMERA, Constant.PERMISSION_CAMERA);
            return false;
        }

        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            System.out.println("req write storage permission*****");
            checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Constant.PERMISSION_WRITE_EXTERNAL_STORAGE);
            return false;
        }
        return true;
    }

首先尝试调用此函数。确保你获得许可。 API23需要这个。单击以启用相机拍照时运行它。您是否能够在没有此功能的情况下启动相机。如果我不在API 23中执行此操作,我的应用会崩溃

答案 1 :(得分:0)

如果data.getData()返回null,请尝试:

        Bundle extras = data.getExtras();
        Bitmap bitmap = (Bitmap) extras.get("data");