Search code examples
androidandroid-intentsamsung-mobile

Crash in Intent.putExtra() on some samsung devices


From my app, I'm opening the Documents with ACTION_GET_CONTENT to get a video. I'm getting a Uri in onActivityResult() and using that to create a new activity in my app. This works generally well except on some samsung devices, at least galaxy S5 and galaxy S5 mini where it crashes in Intent.putExtra() with a NullPointerException.

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == 1 && intent != null && intent.getData() != null) {

        Intent i = new Intent(this, MyActivity.class);

        Uri uri = intent.getData()
        /**
         * Galaxy S5 will crash here
         */
        i.putExtra("uri", uri);
        startActivity(i);
    }
}

Any idea what could be wrong ? The uri itself is not null, if I log it, I get:

content://com.android.providers.media.documents/document/video%3A459

The logs I get are:

2016-02-04 11:05:09.120 ERROR:  AndroidRuntime : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
2016-02-04 11:05:09.120 ERROR:  AndroidRuntime : at android.content.Intent.putExtra(Intent.java:6471)
2016-02-04 11:05:09.120 ERROR:  AndroidRuntime : at com.mbonnin.app.ui.activity.MainActivity.onActivityResult(MainActivity.java:337)

Solution

  • Try this code

    Bundle bundle = new Bundle();
    bundle.putString("uri",uri.toString());
    i.putExtras(bundle);
    

    In the second activity

    Bundle bundle = getIntent().getExtras();
    String uri  = bundle.getString("uri");