[mono-android] Trouble with Android.Graphics
Narcís Calvet
narcis at steema.com
Thu Nov 24 07:44:44 EST 2011
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: monodroid-bounces at lists.ximian.com
[mailto:monodroid-bounces at lists.ximian.com] 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
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
_______________________________________________
Monodroid mailing list
Monodroid at lists.ximian.com
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
More information about the Monodroid
mailing list