0

I am trying to display some pictures from the sd storage in GridView in a Xamarin.Android app.

The only documentation I've found is this: http://docs.xamarin.com/recipes/android/data/files/selecting_a_gallery_image

But it only works for one image, opening two or more pictures gives me an Java.Lang.OutOfMemoryError

on this row:

imageView.SetImageURI (bitmapList [position]);

Found some Android answers here on SO:

https://stackoverflow.com/a/823966/511299

Translated to C#:

public static Bitmap DecodeFile (String s)
{
    try
    {
        s = s.Replace ("file://", "");
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options ();
        o.InJustDecodeBounds = true;

        BitmapFactory.DecodeStream (new System.IO.FileStream (s, System.IO.FileMode.Open), null, o);

        //The new size we want to scale to
        int REQUIRED_SIZE = 70;

        //Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.OutWidth/scale/2>=REQUIRED_SIZE && o.OutHeight/scale/2>=REQUIRED_SIZE)
        {
            scale *= 2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options ();
        o2.InSampleSize = scale;
        return BitmapFactory.DecodeStream (new System.IO.FileStream (s, System.IO.FileMode.Open), null, o2);

    }
    catch (FileNotFoundException e)
    {
    }
    return null;
}

This gives me an

System.IO.IOException: Sharing violation on path  storage/sdcard0/Pictures/MyAppPhotos/44cdbcf0-a488-40b8-98a9-79f3dc8d9deb.jpg

on the line of

return BitmapFactory.DecodeStream (new System.IO.FileStream (s, System.IO.FileMode.Open), null, o2);

Is it possible I access the file twice?

Or is it perhaps because I use FileStream instead of FileInputStream. Trying to use BitmapFactory.DecodeStream requires a System.IO.Stream as parameter, opposed to the example requiring a string :(

More detailed, here is the Android version of decodeStream:

public static Bitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)

while this is all I can find for Xamarin.Android:

public static Bitmap DecodeStream (System.IO.Stream is, Rect outPadding, BitmapFactory.Options opts)
Community
  • 1
  • 1
Sunkas
  • 9,542
  • 6
  • 62
  • 102

1 Answers1

0

Solved it. Used BitmapFactory.DecodeFile(String path, BitmapFactory.Options opts) instead!

Sunkas
  • 9,542
  • 6
  • 62
  • 102