XLabs ImageButton Dynamic Source

ToddKrokowskiToddKrokowski USMember
edited July 2015 in Xamarin.Android

Hi,

I'm using the ImageButton from the XLabs project to a show a thumbnail of an image collected from an Android's camera or its gallery. I have the following code to write an image to disk. The returned path is what I feed to the Image property of the ImageButton.

`
public string WriteImageToDisk (byte[] imageData, ExifLib.ExifOrientation orientation, string vendorName, string workOrderNumber, string seriesIdentifier)
{
if (imageData == null || imageData.Length < 1)
return null;

if (workOrderNumber == null || workOrderNumber.Equals (""))
    return null;

var filename = vendorName.Replace(" ", "_") + "-" + workOrderNumber + "-" + seriesIdentifier + ".jpg";
var docDir = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var path = System.IO.Path.Combine (docDir, filename);

using (FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write)) {
    fs.Write (imageData, 0, imageData.Length);
}

return path;

}
`

And here's the code I'm using to populate the Image property of the ImageButton:

string fullPathToImage = Path.Combine (DependencyService.Get<IFileUtilities> ().GetDocumentsDirectory (), photo.PathToPhoto); imageButton.Image = ImageSource.FromFile (fullPathToImage);

When I run this I get a Resource Not Found Exception relating to the fact that the Source does not exist.

`at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (object)
at Android.App.SyncContext/c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Java.Lang.IRunnable.cs:71
at (wrapper dynamic-method) object.a22fcc68-6586-4d5b-82f8-a4cc533617e5 (intptr,intptr)

--- End of managed exception stack trace ---
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2009)
at android.content.res.Resources.getDrawable(Resources.java:1584)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
`

The weird thing is, if I restart the application - the image shows up without issue. Any ideas? The code I have for setting the Source property in my iOS implementation is identical (in fact that's part of the Xamarin Forms) and works without issue.

Answers

  • ToddKrokowskiToddKrokowski USMember
    edited July 2015

    I'm going to answer my own question. I swear I spent more than a few hours scratching my head over solving this - who knew that all I had to do was ask a question. :smile:

    By changing the ImageButton property from Image to Source, I was able to get the code to work on Android. This did not introduce a regression on the iOS side either.

Sign In or Register to comment.