Nullpointerexcepiton on cursor while selecting photo from gallery on dialog fragment

♀尐吖头ヾ 提交于 2019-12-04 14:07:07

How do you start your Activity and from where? If you pass your result trough your MainActivity, you can try to make a new function in your DialogFragment like this:

public void onMyActivityResult(Context main, int resultCode... an so on){

     main.getContentResolver.......

}

Edit: i have done it in this way:

Get the Activity in onCreate:

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (MainActivity) this.getActivity();
    }

then:

     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == REQUEST_GALLERY && resultCode == Activity.RESULT_OK) {
           ...

            Uri selectedImage = data.getData();
            String path = getRealPathFromURI(selectedImage);
           ...
            reloadImages();

        }
        super.onActivityResult(requestCode, resultCode, data);
        }

and:

 private String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = mActivity.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file
                  // path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        String path = cursor.getString(idx);
        cursor.close();
        return path;
    }
    }

The problem is probably because getActivity() returns null in this line

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

It occurs when your fragment is not attached to an activity. I see 2 possible causes :

  • You call getActivity() too soon in the fragment lifecycle. You need to wait for the invocation of onAttach(Activity) before playing with getActivity()
  • Your fragment has been detached from the activity, check the return of Fragment.isAdded() to ensure your fragment is still attached.

For choosing the Image from Gallery int PICK_IMAGE = 1;

public void getGalleryImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        activity.startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE);
        }

On Activity Result

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                String selectedImagePath = getAbsolutePath(data.getData());
                Log.d("GetImage","Selected Image Path "+selectedImagePath )
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
}

// Get Image Path

public String getAbsoluteImagePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

I have done with this...and it works well

 protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {

        Uri selectedImage = data.getData();
        URI = Uri.parse("file://" + selectedImage);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!