Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Attempt to validate Image file size or measurement before hitting OOM error

ChongLiKenChongLiKen MYMember ✭✭

We are suffering with OOM issues when there is a large image loading via URL or URI, ? Any possible solution that we can intercept the loading process to understand the image sizing and decide to seize loading rather than hitting error. We are trying to get clue from here http://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/ , however any workaround that capable to work on Android & iOS. Thanks in advanced for the help.

Error message that we captured: Failed to allocate a XXX byte allocation with XXX free bytes and XXMB until OOM

Our code, used to load the image.
public class ImageCacheOptimizer
{
static Dictionary<string,ImageSource> cache = new Dictionary<string,ImageSource> ();

static public ImageSource FromFile(String filename)
{
ImageSource retVal = null;
bool hit = cache.TryGetValue (filename, out retVal);

if (!hit) {
    retVal = ImageSource.FromFile (filename);
    cache [filename] = retVal;
}

return retVal;

}

static public ImageSource FromUri(String filename)
{
ImageSource retVal = null;
bool hit = cache.TryGetValue (filename, out retVal);

if (!hit) {
    retVal = new UriImageSource { CachingEnabled = true, CacheValidity = new TimeSpan(1,0,0,0), Uri = new Uri (filename) };
    cache [filename] = retVal;
}

return retVal;

}

Sign In or Register to comment.