Bug 360 - BitmapFactory.Options.InTempStorage is readonly
Summary: BitmapFactory.Options.InTempStorage is readonly
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-08-22 10:59 UTC by Piotr Kryger
Modified: 2013-05-26 22:56 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Piotr Kryger 2011-08-22 10:59:20 UTC
Property Android.Graphics.BitmapFactory.Options.InTempStorage cannot be assigned to. In java it can be set (it's public field - http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inTempStorage). Also, type of this property is IList<byte> - why not byte[]?
Comment 1 Jonathan Pryor 2011-08-22 16:49:42 UTC
Indeed, we should fix that property to have a setter.

As for why it's an IList<byte> and not a byte[], it's for performance!


The problem is this: if the property were a byte[], then whenever you get (or set) the property, the _entire_ array needs to be copied from/to Java. For small arrays, this would probably be fine, but for large arrays, that could be a lot of memory you're copying. Further add in the fact that it's a _property_ (and not a method), implicitly copying (possibly large) arrays would be Bad™. See also the "Properties That Return Arrays" section at: http://msdn.microsoft.com/en-us/library/k2604h5s.aspx

Instead, what we do is the property getter allocates and returns an Android.Runtime.JavaArray<byte> instance (which implements IList<byte>), which provides an indexer which is implemented as JNIEnv.GetArrayItem().

The result is that if you do:

    byte value = instance.InTempStorage [0];

There will only be a JNI invocation to read index 0 of the Java-side byte array, and not a byte[] copy operation.

(Whether this is actually a performance win or not obviously depends upon what exactly you're doing, but it's certainly semantically more correct than always copying the array on every property access, which would have terrible performance & memory implications.)
Comment 4 Jonathan Pryor 2013-05-26 22:56:49 UTC
Fixed in master/6cfb9f60.