// Application ...
Intent i = new Intent();
i.putExtra(EXTRA_FILE_UPLOAD_URIS, mGalleryAdapter.getItems());
Uri[] getItems() { return mItems; }
// Service ...
intent.getParcelableArrayExtra(EXTRA_FILE_UPLOAD_URIS); //works, returns Parcelable[]
Uri[] uris = (Uri[])intent.getParcelableArrayExtra(EXTRA_FILE_UPLOAD_URIS);
// ... Breaks with ClassCastException
Why does the cast to Uri[] break, when Uri is Parcelable?
Unfortunately there is no way to cast like that for arrays in
Java. You will have to iterate your array and cast each object individually.The reason for this is type Safety, the
JVMsimply cannot ensure that the contents of your array can be casted to Uri, without having to iterate thru them, which is why you have to iterate them and cast them individually.Basically because
Parcelablecould be inherited by other Objects, there is no guarantee that the Array contains onlyUriobjects. However casting to a supertype would work since then type safety would be ok.