It’s been a long time since I posted the last article. To be frank, I am busy till neck in work and hardly get time to share my roller coaster ride with Mobility . However, I have a great plan for upcoming blog articles, more on that, later. (Xamarin stuff incoming)
Xamarin documentation helps, sometimes!
Earlier, we had a requirement in one of our app where we had to pick image from gallery / file manager and use it for further processing. This being most implemented and straight forward task, doesn’t look like a big task for Android developers out there. Like good guys, our team went straight to the Xamarin Developer Recipes (https://developer.xamarin.com/recipes/android/other_ux/pick_image/) and found one article. As always, article was really easy and self explanatory. We implemented same code (with some optimizations, obviously). It started working flawlessly on my OnePlus One (no surprises here).
Here is Xamarin’s implementation –
Moto G challange
We were under impression, this code should work flawlessly on other devices as well. We borrowed Moto G and few other phones from colleagues. Started testing my app on those devices. Guess what, We could not get any image from Gallery. The reason why we get path as null *might be* because method ManageQuery is deprecated. We were surprised that Xamarin documentation still refers to method that was deprecated in API 11 (Honeycomb).
We came up with an alternative solution that worked for rest of the devices –
Instead of ManagedQuery, we now use ContentResolver’s Query which queries the URI and returns cursor over result set. We move cursor to first row and try getting document ID, which we later use to resolve one more URI to obtain real path on file system.
Then, there was Touchwiz
Everything was looking fine and we decided to test the app on more complex and heavy Android skins like Touchwiz. This time, we could not split document_id because the value that was returning did not have any colon ( : ), resulting ArgumentOutOfRange exception. To handle that, we added a simple line to check whether document_id can be split with colon or not. Surprisingly, even without split, we could get cursor. Here’s the working code –
While I have started loving Xamarin more and more, I have a suggestion for Xamarin team, please update the documentation. It really needs a quality check.
Hope, this article will help you to fix this issue if at all you get it.