getPackageManger方法在使用openImageIntent方法从相机或gallary中选择图像时出现Nullpointer异常

时间:2015-01-20 10:41:42

标签: android android-intent

我正在调用图像意图方法来获取相机或gallary的选项,同时点击图像视图。我在这里找到了代码 Allow user to select camera or gallery for image 用户David Manpearl

这是我正在使用的代码

public static final int YOUR_SELECT_PICTURE_REQUEST_CODE=222;

 public void openImageIntent1() {

        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "userDir" + File.separator);
        root.mkdirs();
        final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

            // Camera.
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = this.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);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                cameraIntents.add(intent);
            }

            // Filesystem.
            final Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            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, 222);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK) {
                if(requestCode == 222) {
                    final boolean isCamera;
                    if(data == null)
                        isCamera = true;
                    else {
                        final String action = data.getAction();
                        if(action == null)
                            isCamera = false;
                        else
                            isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                    Uri selectedImageUri;
                    if(isCamera)
                        selectedImageUri = outputFileUri;
                    else
                        selectedImageUri = data == null ? null : data.getData();
                }
            }
        }
    }
}

这样,在最终PackageManager packageManager = this.getPackageManager()行上收到错误。

你能帮帮忙吗? 谢谢

1 个答案:

答案 0 :(得分:1)

  

我通过调用此调用的对象

在另一个类中调用此方法

永远不要使用Activity实例化new。它将无法正确初始化,并且调用任何活动或上下文方法将无法正常工作。因此,对于你想要用它的任何东西都没有用。

相反,将Context作为参数传递给需要它的方法。

要实例化某项活动,请将startActivity()Intent一起使用。