Quantcast

Trouble with Android.Graphics

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Trouble with Android.Graphics

jheerman
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.cs:33
  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
  at Android.Runtime.JNIEnv.FindClass (System.String classname)
[0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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.cs:33
  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
  at Android.Runtime.JNIEnv.FindClass (System.String classname)
[0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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.cs:33
  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
  at Android.Runtime.JNIEnv.FindClass (System.String classname)
[0x00000] in /Volumes/Macintosh
HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Jonathan Pryor-2
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.cs:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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.cs:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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.cs:33
>  at Android.Runtime.JNIEnv.get_Env () [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:24
>  at Android.Runtime.JNIEnv.FindClass (System.String classname)
> [0x00000] in /Volumes/Macintosh
> HD/Users/jon/Development/xamarin/monodroid/Mono.Android/src/Runtime/JNIEnv.cs: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
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Andrew Sinclair
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: [hidden email]
[mailto:[hidden email]] 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
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

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

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Jonathan Pryor-2
On Sep 30, 2011, at 4:58 AM, Andrew Sinclair wrote:
> 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)

It's not stream-related, it's wrapped-java-stream related. HttpWebRequest doesn't involve Java streams at all, just mono's normal Stream-atop-libc support, and thus should act normally.

I'm not sure what bug 1031 has to do with HttpWebRequest, though; I don't see HttpWebRequest mentioned, and it's about long GC pauses...

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Andrew Sinclair
OK, thanks for the clarification.

That freeze happens during a session of HttpWebRequests. The "stacktrace:"
message shown sometimes indicates that the crash is within WebAsyncResult.AB
but more usually it's within the processing (eg. decompression) of the
response. So I wondered whether a stream-related glitch might be causing it,
but clearly it's not.

NB. Prior to 1.2.0 I did see general long pauses throughout the app, these
have now gone. The issue recorded in this new bug (1.2.0 and 1.9.0) is not a
pause - the app just stops and doesn't continue (and doesn't die either.)
The only "smoking gun" is the mysterious "stacktrace:" message which is
being generated for some reason (by Mono?) but which doesn't result in an
application exception or termination.

Andy

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

On Sep 30, 2011, at 4:58 AM, Andrew Sinclair wrote:
> 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)

It's not stream-related, it's wrapped-java-stream related. HttpWebRequest
doesn't involve Java streams at all, just mono's normal Stream-atop-libc
support, and thus should act normally.

I'm not sure what bug 1031 has to do with HttpWebRequest, though; I don't
see HttpWebRequest mentioned, and it's about long GC pauses...

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

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

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Jonathan Pryor-2
The one issue I know of with HttpWebRequest is that ~everything related to it _must_ be disposed of in an orderly fashion, as having multiple requests "in flight" at once tends to make things break. See also the epic comment at:

        https://bugzilla.novell.com/show_bug.cgi?id=648862#c9

For reference, not disposing of the HttpWebResponse instance causes things to break weirdly even in a console mono app; this is in no way limited to Mono for Android.

Now, you may be doing everything right; I have no idea. This is just an FYI. :-)

 - Jon

On Sep 30, 2011, at 10:11 AM, Andrew Sinclair wrote:

> OK, thanks for the clarification.
>
> That freeze happens during a session of HttpWebRequests. The "stacktrace:"
> message shown sometimes indicates that the crash is within WebAsyncResult.AB
> but more usually it's within the processing (eg. decompression) of the
> response. So I wondered whether a stream-related glitch might be causing it,
> but clearly it's not.
>
> NB. Prior to 1.2.0 I did see general long pauses throughout the app, these
> have now gone. The issue recorded in this new bug (1.2.0 and 1.9.0) is not a
> pause - the app just stops and doesn't continue (and doesn't die either.)
> The only "smoking gun" is the mysterious "stacktrace:" message which is
> being generated for some reason (by Mono?) but which doesn't result in an
> application exception or termination.
>
> Andy
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Jonathan Pryor
> Sent: 30 September 2011 14:55
> To: Discussions related to Mono for Android
> Subject: Re: [mono-android] Trouble with Android.Graphics
>
> On Sep 30, 2011, at 4:58 AM, Andrew Sinclair wrote:
>> 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)
>
> It's not stream-related, it's wrapped-java-stream related. HttpWebRequest
> doesn't involve Java streams at all, just mono's normal Stream-atop-libc
> support, and thus should act normally.
>
> I'm not sure what bug 1031 has to do with HttpWebRequest, though; I don't
> see HttpWebRequest mentioned, and it's about long GC pauses...
>
> - Jon
>
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
>
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Narcís Calvet
In reply to this post by Jonathan Pryor-2
Hi Jon,

>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.

Has this been fixed? I'm having problems compressing a bitmap as shown in
the method below. DecodeStream returns null and I wonder if it's due to:

1. DecodeStream not supporting compressed bitmaps.
2. Wrong stream usage.
3. A Monodroid bug mapping .NET and Java streams.

    private static Android.Graphics.Bitmap
CompressToPNG(Android.Graphics.Bitmap bmp)
    {
      try
      {
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        if (bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100,
stream))
        {
          //bmp.Recycle();
          Android.Graphics.Bitmap b =
Android.Graphics.BitmapFactory.DecodeStream(stream); //DecodeStream works
with compressed bitmaps?
          //Android.Graphics.Bitmap b =
Android.Graphics.BitmapFactory.DecodeByteArray(stream.GetBuffer(), 0,
(int)stream.Length);

          stream.Close();
          stream.Dispose();
          return (b == null) ? bmp : b;
        }
        else return bmp;
      }
      catch (Exception e)
      {
        System.Diagnostics.Debug.WriteLine(e.Message);
        return null;
      }
    }

Can you shed any light on this? Happy thanksgiving to the Americans.

Thanks in advance,
 
Narcís Calvet
Steema Software
http://www.steema.com 
http://twitter.com/SteemaSoftware 
https://www.facebook.com/SteemaSoftware

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Jonathan Pryor
Sent: dijous, 29 / setembre / 2011 23: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
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

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


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Narcís Calvet
Any update on the email below?

Thanks in advance,
Narcís Calvet

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Narcís Calvet
Sent: dijous, 24 / novembre / 2011 13:45
To: 'Discussions related to Mono for Android'
Subject: Re: [mono-android] Trouble with Android.Graphics

Hi Jon,

>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.

Has this been fixed? I'm having problems compressing a bitmap as shown in
the method below. DecodeStream returns null and I wonder if it's due to:

1. DecodeStream not supporting compressed bitmaps.
2. Wrong stream usage.
3. A Monodroid bug mapping .NET and Java streams.

    private static Android.Graphics.Bitmap
CompressToPNG(Android.Graphics.Bitmap bmp)
    {
      try
      {
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        if (bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100,
stream))
        {
          //bmp.Recycle();
          Android.Graphics.Bitmap b =
Android.Graphics.BitmapFactory.DecodeStream(stream); //DecodeStream works
with compressed bitmaps?
          //Android.Graphics.Bitmap b =
Android.Graphics.BitmapFactory.DecodeByteArray(stream.GetBuffer(), 0,
(int)stream.Length);

          stream.Close();
          stream.Dispose();
          return (b == null) ? bmp : b;
        }
        else return bmp;
      }
      catch (Exception e)
      {
        System.Diagnostics.Debug.WriteLine(e.Message);
        return null;
      }
    }

Can you shed any light on this? Happy thanksgiving to the Americans.

Thanks in advance,
 
Narcís Calvet
Steema Software
http://www.steema.com 
http://twitter.com/SteemaSoftware 
https://www.facebook.com/SteemaSoftware

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Jonathan Pryor
Sent: dijous, 29 / setembre / 2011 23: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
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

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


_______________________________________________
Monodroid mailing list
[hidden email]

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


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Jonathan Pryor-2
In reply to this post by Narcís Calvet
On Nov 24, 2011, at 7:44 AM, Narcís Calvet wrote:
> Has this been fixed? I'm having problems compressing a bitmap as shown in
> the method below. DecodeStream returns null and I wonder if it's due to:

It's a bug in your code. When you write to a stream, the Position of the stream is at end-of-stream:

>        System.IO.MemoryStream stream = new System.IO.MemoryStream();
>        if (bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, stream))
>        {
>          Android.Graphics.Bitmap b = Android.Graphics.BitmapFactory.DecodeStream(stream);

Notice that you don't rewind the stream, so the stream you're passing to Java is at EOF, and thus Java reads a 0-length stream, and does the only sane thing it can: return null.

Here's an Activity that Works For Me™ (though this is also tested with what should be the next release, so if this doesn't work for you...it'll be fixed in the next release ;-)

        [Activity (Label = "Scratch.Graphics", MainLauncher = true)]
        public class Activity1 : Activity
        {
                int count = 1;

                protected override void OnCreate (Bundle bundle)
                {
                        base.OnCreate (bundle);

                        // Set our view from the "main" layout resource
                        SetContentView (Resource.Layout.Main);

                        // Get our button from the layout resource,
                        // and attach an event to it
                        Button button = FindViewById<Button> (Resource.Id.myButton);

                        button.Click += delegate {
                                button.Text = string.Format ("{0} clicks!", count++); };
                       
                        var source = BitmapFactory.DecodeResource (Resources, Resource.Drawable.Icon);
                        Log.Info ("*jonp*", "source? {0}", source != null);
                        var dest = CompressToPNG (source);
                        Log.Info ("*jonp*", "dest? {0}", dest != null);
                }

                private static Android.Graphics.Bitmap CompressToPNG (Android.Graphics.Bitmap bmp)
                {
                        try {
                                using (var stream = new System.IO.MemoryStream()) {
                                        if (bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, stream)) {
                                                stream.Position = 0;
                                                var b = Android.Graphics.BitmapFactory.DecodeStream(stream);
                                                Log.Info ("*jonp*", "decoded stream? {0}", b != null);
                                                return (b == null) ? bmp : b;
                                        }
                                        else
                                                return bmp;
                                }
                        }
                        catch (Exception e)
                        {
                                System.Diagnostics.Debug.WriteLine(e);
                                return null;
                        }
                }
        }

The output when I run this:

        I/*jonp*  (31153): source? True
        I/*jonp*  (31153): decoded stream? True
        I/*jonp*  (31153): dest? True

Notice that the "decoded stream" in CompressToPNG() is a non-null value.

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Narcís Calvet
Hi Jon,

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Jonathan Pryor
Sent: dimecres, 30 / novembre / 2011 15:40
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Trouble with Android.Graphics
>
>It's a bug in your code. When you write to a stream, the Position of the
stream is at end-of-stream:
>
>>        System.IO.MemoryStream stream = new System.IO.MemoryStream();
>>        if (bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100,
stream))
>>        {
>>          Android.Graphics.Bitmap b =
Android.Graphics.BitmapFactory.DecodeStream(stream);
>
>Notice that you don't rewind the stream, so the stream you're passing to
Java is at EOF, and thus Java reads a 0->length stream, and does the only
sane thing it can: return null.

Thanks! Actually I tried that before but this method is giving memory
consumption problems as well. I try compressing images to save memory but
objects used in this method consume more memory than what it's freed so I
compression code is working fine but sometimes fails to complete due to the
lack of memory. Any advice on that? I tried Bitmap.Recycle() and disposing
used streams without success.

Best Regards,
 
Narcís Calvet
Steema Software
http://www.steema.com 
http://twitter.com/SteemaSoftware 
https://www.facebook.com/SteemaSoftware 


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Jonathan Pryor-2
On Dec 1, 2011, at 5:13 AM, Narcís Calvet wrote:
> Thanks! Actually I tried that before but this method is giving memory
> consumption problems as well. I try compressing images to save memory but
> objects used in this method consume more memory than what it's freed so I
> compression code is working fine but sometimes fails to complete due to the
> lack of memory. Any advice on that?

Toss in a GC.Collect() for good measure (after recycling and disposing of the Bitmap instances).

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trouble with Android.Graphics

Narcís Calvet
Hi Jon,

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Jonathan Pryor
Sent: dijous, 1 / desembre / 2011 12:57
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Trouble with Android.Graphics
>
>Toss in a GC.Collect() for good measure (after recycling and disposing of
the Bitmap instances).

Not very elegant but it certainly helps :P. Thanks!


Best Regards,
 
Narcís Calvet
Steema Software
http://www.steema.com 
http://twitter.com/SteemaSoftware 
https://www.facebook.com/SteemaSoftware 






_______________________________________________
Monodroid mailing list
[hidden email]

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