java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()'

断了今生、忘了曾经 提交于 2019-12-07 09:34:12

问题


I am Trying To upload image to server from gallery and camera.When i Choose image from gallery to upload to server its Work Perfactly.But when i Select camera captured Image for upload to server Its Crash And Showing the error.

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

At 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 the error on this line in onActivityResult

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

The method i used for open camera on button click.

  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 don't know How t get this error even after i used is already in my Previous PROJECT.

THANKS in Advance


回答1:


I had same issue when working with Camera api and Intent of onActivityResult, finally I found out it depends on your android device version, in Android M (Marshmallow - 6.0–6.0.1 ) onActivityResult intent is like below :

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

and for android versions lower than Marshmallow:

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

I think you must check android version onActivityResult and the for android versions Marshmallow to get direct file from path in the Uri and 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
    }

I hope it can be useful for you.




回答2:


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



回答3:


I meet this problem either, after many test, I found its because I set the attribute named android:windowIsTranslucent=true, if I delete this attribute from style, it always work.



来源:https://stackoverflow.com/questions/32564041/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-java-lang-stri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!