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

狂风中的少年 提交于 2019-12-05 13:21:59

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.

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

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.

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