java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.net.Uri.getScheme()'

时间:2015-09-14 11:47:37

标签: java android image upload server

我正在尝试从图库和相机上传图像到服务器。当我选择图库中的图像上传到服务器时它的工作可能。但是当我选择相机捕获的图像上传到服务器时崩溃并显示错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference

OnActivityResult。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            decodeFile(picturePath);
            new ImageUploadTask().execute();
        }else {

            Toast.makeText(getApplicationContext(), "User Canceled",
                    Toast.LENGTH_LONG).show();
        }
    }

我在onActivityResult

中遇到了这一行的错误
 Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

按钮点击时用于打开相机的方法。

  loadimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                AlertDialog.Builder builder = new AlertDialog.Builder(Add_Info.this);
                builder.setMessage("Select Image From")
                        .setCancelable(true)
                        .setPositiveButton("Camera", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(intent, CAMERA_REQUEST);
                                mImageCaptureUri = Uri.fromFile(new File(Environment
                                        .getExternalStorageDirectory(), "tmp_avatar_"
                                        + String.valueOf(System.currentTimeMillis())
                                        + ".jpg"));
                            }
                        })
                        .setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(
                                        Intent.ACTION_PICK,
                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                                startActivityForResult(i, RESULT_LOAD_IMAGE);

                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();


            }
        });

帮助我解决这个问题。我不知道即使我使用过这个错误之后也是如此。

感谢提前

3 个答案:

答案 0 :(得分:2)

我在使用Camera api和onActivityResult的Intent时遇到了同样的问题,最后我发现它取决于你的Android设备版本,在Android M(Marshmallow - 6.0-6.0.1)中onActivityResult意图如下:

requestCode : 230 resultCode : -1 intent :Intent{dat=file:///storage/emulated/0/Pictures/Rahnama/IMG_20160508_121332_1519065    564.jpg typ=image/jpeg } RESULT_OK : -1

和低于Marshmallow的Android版本:

requestCode : 230 resultCode : -1 intent : Intent { dat=content://media/external/images/media/309 (has extras) } RESULT_OK : -1

我认为你必须检查Android版本onActivityResult和Android版本Marshmallow从Uri和Intent中的路径获取直接文件:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M){
            final Uri data = intent.getData();
            final File file = new File(data.getPath());
            // now you can upload your image file
    }else{
           // in android version lower than M your method must work
    }

我希望它对你有用。

答案 1 :(得分:0)

我遇到了这个问题,经过多次测试,我找到了它,因为我设置了名为android:windowIsTranslucent=true的属性,如果我从样式中删除了这个属性,它总能工作。

答案 2 :(得分:0)

if (null != account.getPhotoUrl()) {
    // Write code here
}