[英]How get images from external storage
I have created a folder called Items
in /mnt/sdcard/
, from where I want to find JPG
images. 我在
/mnt/sdcard/
创建了一个名为Items
的文件夹,从中我想从中找到JPG
图像。 Then I want to display a ListView
with all the names of the listed images. 然后,我想显示具有列出图像的所有名称的
ListView
。 Upon clicking on one of the names in the list, I would like the image from the path display in an ImageView
. 单击列表中的名称之一后,我想要
ImageView
路径显示中的ImageView
。 I have troubles finding the images. 我找不到图像。
How can I do this? 我怎样才能做到这一点?
The External Storage path differs from device to device and I strongly suggest you are not using /mnt/sdcard/
but rater Environment.ExternalStorageDirectory
. 外部存储路径因设备而异,我强烈建议您不要使用
/mnt/sdcard/
而是使用ratingr Environment.ExternalStorageDirectory
。
You should be able to use normal C# file operations to get a list of files. 您应该能够使用正常的C#文件操作来获取文件列表。
string[] filePaths = Directory.GetFiles(Environment.ExternalStorageDirectory, "*.jpg");
You can use the filePaths to pass to your custom Adapter
and inside that load the Bitmap
s if you want to display them inside of the ListView
: 如果要在
ListView
内显示它们,可以使用filePath传递给自定义的Adapter
并在内部加载Bitmap
:
using(var bitmap = BitmapFactory.DecodeFile(filePaths[position]))
imageView.SetImageBitmap(bitmap);
or you can simply use a SimpleAdapter
and pass it the filePaths
, which will then display them as strings. 或者,您可以简单地使用
SimpleAdapter
并将其传递给filePaths
,然后将其显示为字符串。
Then you just need to hook up the ItemClick
event to get the position in the list clicked and load the correct Bitmap
into an ImageView. 然后,您只需要挂接
ItemClick
事件即可获得单击列表中的位置,然后将正确的Bitmap
加载到ImageView中。
Also please read http://docs.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently if you are using large images, as you have very limited resources. 另外,如果您使用的是大型图片,则请阅读http://docs.xamarin.com/recipes/android/resources/general/load_large_bitmaps_效率 ,因为您的资源非常有限。
A good resource for Custom List Adapters: http://redth.info/2010/10/12/monodroid-custom-listadapter-for-your-listview/ 自定义列表适配器的一个很好的资源: http : //redth.info/2010/10/12/monodroid-custom-listadapter-for-your-listview/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.