在GridView中显示sd卡的图片
本文关键字:sd GridView 显示 | 更新日期: 2023-09-27 18:05:51
我试图在Xamarin中显示GridView中sd存储的一些图片。Android应用程序。
我找到的唯一文档是:http://docs.xamarin.com/recipes/android/data/files/selecting_a_gallery_image
但是它只适用于一张图片,打开两张或更多的图片会给我一个Java.Lang.OutOfMemoryError
在这一行:
imageView.SetImageURI (bitmapList [position]);
在这里找到一些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
作为参数,而不是需要字符串的示例:(
更详细,这里是decodeStream
的Android版本:
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)
解决了。用BitmapFactory.DecodeFile(String path, BitmapFactory.Options opts)
代替!