您應該添加網格視圖Adapter類。
public class GalleryPictureActivity extends Activity
{
private String[] FilePathStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
public static Bitmap bmp = null;
ImageView imageview;
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_picture);
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
Toast.makeText(this, "Error! No SDCARD Found!",
Toast.LENGTH_LONG).show();
}
else
{
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/images");
}
if (file.isDirectory())
{
listFile = file.listFiles();
FilePathStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
FilePathStrings[i] = listFile[i].getAbsolutePath();
}
}
grid = (GridView)findViewById(R.id.gridview);
adapter = new GridViewAdapter(this, FilePathStrings);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view,
int position, long id)
{
imageview = (ImageView)findViewById(R.id.imageView1);
int targetWidth = 700;
int targetHeight = 500;
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(FilePathStrings[position],
bmpOptions);
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;
int sampleSize = 1;
if (currHeight > targetHeight || currWidth > targetWidth)
{
if (currWidth > currHeight)
sampleSize = Math.round((float)currHeight
/(float)targetHeight);
else
sampleSize = Math.round((float)currWidth
/(float)targetWidth);
}
bmpOptions.inSampleSize = sampleSize;
bmpOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(FilePathStrings[position],
bmpOptions);
imageview.setImageBitmap(bmp);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
bmp = null;
}
});
}
}
另一個類的GridView適配器:
public class GridViewAdapter extends BaseAdapter
{
private Activity activity;
private String[] filepath;
private static LayoutInflater inflater = null;
Bitmap bmp = null;
public GridViewAdapter (Activity a, String[] fpath)
{
activity = a;
filepath = fpath;
inflater = (LayoutInflater)activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return filepath.length;
}
public Object getItem (int position)
{
return position;
}
public long getItemId (int position)
{
return position;
}
public View getView (int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
ImageView image = (ImageView)vi.findViewById(R.id.image);
int targetWidth = 100;
int targetHeight = 100;
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath[position], bmpOptions);
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;
int sampleSize = 1;
if (currHeight > targetHeight || currWidth > targetWidth)
{
if (currWidth > currHeight)
sampleSize = Math.round((float)currHeight
/(float)targetHeight);
else
sampleSize = Math.round((float)currWidth
/(float)targetWidth);
}
bmpOptions.inSampleSize = sampleSize;
bmpOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(filepath[position], bmpOptions);
image.setImageBitmap(bmp);
image.setScaleType(ImageView.ScaleType.FIT_XY);
bmp = null;
return vi;
}
}
活動:
activity_gallery_picture:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight=".85">
</GridView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:scaleType="fitXY"
android:src="@drawable/galleryimage" />
</LinearLayout>
另一個活動佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
文你給一個靜態路徑ü不需要調用,您的使用意圖 –
你說,我不需要內部onScanCompleted代碼試圖畫廊? – Rafael
:是的..動作視圖不會給你你想要做的靜態路徑。 –