我试图在Xamarin.Android应用程序中显示GridView中sd存储的一些图片。
我发现的唯一文档是:http://docs.xamarin.com/recipes/android/data/files/selecting_a_gallery_image
但它仅适用于一张图片,打开两张或多张图片会给我Java.Lang.OutOfMemoryError
在这一行:
imageView.SetImageURI (bitmapList [position]);
在SO上找到了一些Android答案:
https://stackoverflow.com/a/823966/511299
转换为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;
}
这给了我一个
System.IO.IOException: Sharing violation on path storage/sdcard0/Pictures/MyAppPhotos/44cdbcf0-a488-40b8-98a9-79f3dc8d9deb.jpg
在
行return BitmapFactory.DecodeStream (new System.IO.FileStream (s, System.IO.FileMode.Open), null, o2);
我可以两次访问该文件吗?
或者是因为我使用FileStream
代替FileInputStream
。尝试使用BitmapFactory.DecodeStream
需要System.IO.Stream
作为参数,与需要字符串的示例相反:(
更详细,这是Android版decodeStream
:
public static Bitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)
虽然这是我能找到的所有Xamarin.Android:
public static Bitmap DecodeStream (System.IO.Stream is, Rect outPadding, BitmapFactory.Options opts)
答案 0 :(得分:0)
解决了它。改为使用BitmapFactory.DecodeFile(String path, BitmapFactory.Options opts)
!