android 用于拍摄照片或视频的文件选择器或相机在 nexus 4 上不可用

an android file picker or camera for taking pictures or videos not working on nexus 4

我正在尝试使用以下代码打开 android 文件选择器或相机以拍摄照片或视频:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICKFILE_RESULT_CODE && Activity.RESULT_OK == resultCode) {
            try {
                // Get the Uri of the selected file 
                Uri uri = data.getData();
                LogS.d("File Uri: " + uri.toString());
                // Get the path
                String path = BeanUtils.getPath(getActivity().getApplicationContext(), uri);

                AnnexFile f = new AnnexFile(path);
                if (!addedFiles.contains(f)) {
                    addFileToLayout(f);
                    addedFiles.add(f);
                } else {
                    Toast.makeText(getActivity(), R.string.you_allready_added_this_file_, Toast.LENGTH_SHORT).show();;
                }
            } catch (Exception e) {
                LogS.e(e);
                Toast.makeText(getActivity(), getString(R.string.file_manager_invalid), Toast.LENGTH_LONG).show();
            }
        } else if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && Activity.RESULT_OK == resultCode) {
            try {
                Uri uri = data.getData();
                LogS.d("File Uri: " + uri.toString());
                // Get the path
                String path = BeanUtils.getPath(getActivity().getApplicationContext(), uri);

                AnnexFile f = new AnnexFile(path);
                if (!addedFiles.contains(f)) {
                    addFileToLayout(f);
                    addedFiles.add(f);
                } else {
                    Toast.makeText(getActivity(), R.string.you_allready_added_this_file_, Toast.LENGTH_SHORT).show();;
                }
            } catch (Exception e) {
                LogS.e(e);
                Toast.makeText(getActivity(), getString(R.string.file_manager_invalid), Toast.LENGTH_LONG).show();
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void openImageIntent() {

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getActivity().getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam){
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        cameraIntents.add(intent);
    }

    final Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    final List<ResolveInfo> listVideoCam = packageManager.queryIntentActivities(videoIntent, 0);
    for (ResolveInfo res : listVideoCam){
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(videoIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        cameraIntents.add(intent);
    }

    //FileSystem
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*;video/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
    startActivityForResult(chooserIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

该代码在 Samsung S4 和 Sony Experia J 上运行完美,但在 Nexus 4 (android 5.1.1) 上运行失败。当我在 nexus 4 上调试应用程序时,我发现如果最终用户尝试制作图片,则以下 Uri uri = data.getData(); 为 null。如果用户尝试制作视频或打开现有媒体文件,该应用程序适用于所有设备。

When I debugged the application on nexus 4 I found that the following Uri uri = data.getData(); is null if the end user tries to make a picture

您假设 ACTION_IMAGE_CAPTURE return 是 Uri 形式的结果。是not documented to do so。有数以千计的相机应用程序,预装和用户安装。他们中的许多人会宣传支持 ACTION_IMAGE_CAPTURE。 None 必须 return 一个 Uri 结果。