直接上代码

1.Layout——Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="@string/Welcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <Gallery
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery1" />
</LinearLayout>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

2.Activity1.cs

using System;
 
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Database;
using Android.Provider;
 
namespace GalleryViewDemo
{
    [Activity(Label = "Gallery View Sample", MainLauncher = true, Icon = "@drawable/icon")]
    public class GalleryViewSample : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            CreateGallery();
        
        }
 
 
        private void CreateGallery()
        {
            Gallery g = this.FindViewById<Gallery>(Resource.Id.gallery1);
            g.Adapter = new ImageAdapter(this);
        }
    }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

3.ImageAdapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Database;
using Android.Provider; namespace GalleryViewDemo
{
public class ImageAdapter : BaseAdapter
{
Context _Context;
ICursor _ImageCursor;
protected ICursor ImageCursor
{
get
{
if (_ImageCursor == null)
{
_ImageCursor = GetImageCursor();
}
return _ImageCursor;
}
set
{
_ImageCursor = value;
}
}
public ImageAdapter(Context c)
{
_Context = c;
}
private ICursor GetImageCursor()
{
string[] Projection = { MediaStore.Images.Thumbnails.ImageId };
var ImageCursor = ((Activity)_Context).ManagedQuery(MediaStore.Images.Thumbnails.ExternalContentUri,
Projection, null, null, null);
return ImageCursor;
}
public override int Count
{
get { return ImageCursor.Count; }
} public override Java.Lang.Object GetItem(int position)
{
return position;
} public override long GetItemId(int position)
{
ImageCursor.MoveToPosition(position);
var ImageId = ImageCursor.GetString(0);
return position;
} public override View GetView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
ImageView ReturnView = new ImageView(_Context);
ImageCursor.MoveToPosition(position); var ImageId = ImageCursor.GetString(0);
ReturnView.SetImageURI(Android.Net.Uri.WithAppendedPath(MediaStore.Images.Thumbnails.ExternalContentUri, ImageId));
ReturnView.SetScaleType(ImageView.ScaleType.CenterCrop);
return ReturnView;
}
else
{
return(ImageView) convertView;
}
}
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

在模拟器中,先下载了两幅图片,运行成功。

在手机上运行失败,尚不知道原因,求解。

04-18 17:40