Jon,

Given that this is a general stream-related issue, could it be causing the
random issues I've seen around HttpWebRequests?
(http://bugzilla.xamarin.com/show_bug.cgi?id=1031)

Thanks,

Andy

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jonathan Pryor
Sent: 29 September 2011 22:23
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Trouble with Android.Graphics

Short version:

        
http://stackoverflow.com/questions/7535503/mono-for-android-exit-code-255-on
-bitmapfactory-decodestream/7590929#7590929

Long version:
We map every Java method which takes e.g. java.io.InputStream to a
System.IO.Stream, and provide an "Adapter" which subclasses System.IO.Stream
and invokes the corresponding InputStream methods. Ditto for OutputStream,
etc.

However, one thing we didn't realize until this week is that Java and C#
have different conventions for "end-of-stream" (EOS) -- Java uses -1, while
C# uses 0.

So what would frequently happen (such as your test cases) is that you'd call
the bound method, which would return a Stream, and then invoke another Java
method. This would result in e.g. BitmapDrawable.createFromStream() doing a
Java->Managed->Java transition for the .read() method, and because of the
"mismatch" of EOS conventions, things would go "weird" (e.g. the Java stream
would return 0, which C# code would interpret as EOS, when it actually meant
"no data right now, try again later.")

This will be fixed in the 1.9.1 release. In the meantime, try going through
a MemoryStream + byte[], as I suggest in the above stack overflow answer.

 - Jon

On Sep 29, 2011, at 12:30 PM, John Heerman wrote:

> Hello,
> 
> I've run across a problem trying to retrieve an image from the web and
> decode the stream or byte array into a bitmap/drawable resource.  Each
> one of the tests below fail when I try to invoke the static
> BitmapFactory or BitmapDrawable class.  I'm running my tests in
> MonoDevelop on Mac OSX Lion targeting API Level 8 with Mono for
> Android 1.2.
> 
> The exception I receive for test 1:
> System.NullReferenceException: Object reference not set to an instance
> of an object
>  at Android.Runtime.JNIEnv.get_Handle () [0x00014] in
> /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:145
>  at Android.Graphics.BitmapFactory..cctor () [0x00000] in <filename
unknown>:0
> 
> 
> The exception I receive for test 2:
> System.NullReferenceException: Object reference not set to an instance
> of an object
>  at Android.Runtime.JNIEnv.get_Handle () [0x00014] in
> /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:145
>  at Android.Graphics.BitmapFactory..cctor () [0x00000] in <filename
unknown>:0
> 
> The exception I receive for test 3:
> System.NullReferenceException: Object reference not set to an instance
> of an object
>  at Android.Runtime.JNIEnv.get_Handle () [0x00014] in
> /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
>
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.c
s:145
>  at Android.Graphics.Drawables.Drawable..cctor () [0x00000] in
> <filename unknown>:0
> 
> Any help would be greatly appreciated.
> 
> Regards,
> John
> 
> //////////////////////////////////////////////////////////
> 
> using System;
> using System.Net;
> 
> using Android.Graphics;
> using Android.Graphics.Drawables;
> 
> using NUnit.Framework;
> 
> namespace App.Shared.Test
> {
>       [TestFixture]
>       public class ImageTest
>       {
>               private string url =
> "http://www.google.com/intl/en_com/images/srpr/logo3w.png";;
>               
>               [Test]
>               public void TestStreamWithBitmapFactory()
>               {
>                       try
>                       {
>                               System.IO.Stream stream = new
WebClient().OpenRead(url);
>                               var bitmap =
BitmapFactory.DecodeStream(stream);
>                               Assert.IsNotNull(bitmap);
>                       }
>                       catch (Exception ex)
>                       {
>                               Console.WriteLine(ex.InnerException);
>                               Assert.Fail(ex.InnerException.ToString());
>                       }
>                       
>               }
>               
>               [Test]
>               public void TestBytesWithBitmapFactory()
>               {
>                       try
>                       {
>                               byte[] bytes = new
WebClient().DownloadData(url);
>                               var bitmap =
BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
>                               Assert.IsNotNull(bitmap);
>                       }
>                       catch (Exception ex)
>                       {
>                               Console.WriteLine(ex.InnerException);
>                               Assert.Fail(ex.InnerException.ToString());
>                       }
>               }
>               
>               [Test]
>               public void TestStreamWithBitmapDrawable()
>               {
>                       try
>                       {
>                               System.IO.Stream stream = new
WebClient().OpenRead(url);
>                               var bitmap =
BitmapDrawable.CreateFromStream(stream, "src");
>                               Assert.IsNotNull(bitmap);
>                       }
>                       catch (Exception ex)
>                       {
>                               Console.WriteLine(ex.InnerException);
>                               Assert.Fail(ex.InnerException.ToString());
>                       }
>               }
>       }
> }
> _______________________________________________
> Monodroid mailing list
> [email protected]
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to