Java.lang.NullPointerException: attempt to call the virtual method 'java.lang.String android.net.Uri.getScheme ()'

I am trying to upload an image to the server from the gallery and camera. When I select an image from the gallery to upload my work Perfactly to the server. But when I select a camera, the captured image is uploaded to the server. Failure and error display.

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

In 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();
        }
    }

I got an error in this line in onActivityResult

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

The method that I used for an open camera when I clicked a button.

  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();


            }
        });

Help me solve this problem. I dont know. How to get this error even after I used it already in my previous PROJECT.

RESCUE in Advance

+4
3

Camera api Intent of onActivityResult, , , 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

Android , Marshmallow:

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
    }

, .

+2

, , android:windowIsTranslucent=true, , .

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

Source: https://habr.com/ru/post/1607263/


All Articles