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
Monodroid@lists.ximian.com

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

Reply via email to