onActivityResult:空对象引用上的android.net.Uri android.content.Intent.getData()'

时间:2018-12-19 10:50:36

标签: android image nullpointerexception uri onactivityresult

当我单击相机中的图像或从相机中按回时,它可以完美工作。

当我从图库中选择图像时,Ii也可以很好地工作,但是当我从图库中向后按而不在此行filePath = data.getData();中选择任何图像时,它将发生错误。

  

由于:java.lang.NullPointerException:尝试调用虚拟   null上的方法'android.net.Uri android.content.Intent.getData()'   对象引用

private void changeProfileImage() {
        try {
            PackageManager pm = getPackageManager();
            int hasPerm = pm.checkPermission(Manifest.permission.CAMERA, getPackageName());
            if (hasPerm == PackageManager.PERMISSION_GRANTED) {
                final CharSequence[] options = {"Take Photo", "Choose From Gallery", "Cancel"};
                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(AddProduct.this);
                builder.setTitle("Select Option");
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals("Take Photo")) {
                            dialog.dismiss();
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(intent, PICK_IMAGE_CAMERA);
                        } else if (options[item].equals("Choose From Gallery")) {
                            dialog.dismiss();
                            Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(pickPhoto, PICK_IMAGE_GALLERY);
                        } else if (options[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();
            } else Toast.makeText(this, "Camera Permission error", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "Camera Permission error", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }    

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICK_IMAGE_CAMERA) {
                try {
                    bitmap = (Bitmap) data.getExtras().get("data");
                    pro_img.setImageBitmap(bitmap);
                    img = getEncoded64ImageStringFromBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == PICK_IMAGE_GALLERY) {
                try {
                    filePath = data.getData();
                    if (filePath != null) {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                        pro_img.setImageBitmap(bitmap);
                        img = getEncoded64ImageStringFromBitmap(bitmap);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

是的,如果您没有选择任何图像而只是从Gallery中返回,它将在Intent Data中返回null。检查数据!=空,然后再获取getData()

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICK_IMAGE_CAMERA) {
                try {
                    bitmap = (Bitmap) data.getExtras().get("data");
                    pro_img.setImageBitmap(bitmap);
                    img = getEncoded64ImageStringFromBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == PICK_IMAGE_GALLERY) {
                try {
                      if(data!=null)
                     { // user selects some Image
                    filePath = data.getData();
                    if (filePath != null) {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                        pro_img.setImageBitmap(bitmap);
                        img = getEncoded64ImageStringFromBitmap(bitmap);
                    }
                   }
                   else
                   { // user simply backpressed from gallery
                   }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }