2

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)
mbonnin
  • 6,893
  • 3
  • 39
  • 55
  • There isn't any `putExtra` method in `Intent` which accepts `Uri`, how does you code work on other phones should be the concern. Try to send the data as `String` as suggested by @anshuljain – Rohit5k2 Feb 04 '16 at 19:06
  • 1
    @Rohit5k2 but there is one that accepts a `Parcelable` which `Uri` implements. – George Mulligan Feb 04 '16 at 19:10

1 Answers1

3

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");
thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61