如何在gridview上添加图像?

时间:2011-12-15 06:06:06

标签: android gridview imageview

在下面的gridview image.png图片来自drawable文件夹。现在我希望照片库中的图像或捕获的摄像机图像在此网格中动态添加。如何做?

任何示例代码?

HelloGridView类:

package com.example.HelloGridView; 

import android.app.Activity; 
import android.os.Bundle; 

public class HelloGridView extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    GridView gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageAdapter(this)); 

    gridview.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); 
        } 
    }); 
    } 

ImageAdapter类:

package com.example.HelloGridView; 

public class ImageAdapter extends BaseAdapter { 
private Context mContext; 

public ImageAdapter(Context c) { 
mContext = c; 
} 

public int getCount() { 
return mThumbIds.length; 
} 

public Object getItem(int position) { 
return null; 
} 

public long getItemId(int position) { 
return 0; 
} 

// create a new ImageView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
ImageView imageView; 
if (convertView == null) {  // if it's not recycled, initialize some attributes 
    imageView = new ImageView(mContext); 
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setPadding(8, 8, 8, 8); 
} else { 
    imageView = (ImageView) convertView; 
} 

imageView.setImageResource(mThumbIds[position]); 
return imageView; 
} 

// references to our images 
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, 
R.drawable.sample_6, R.drawable.sample_7, 
R.drawable.sample_0, R.drawable.sample_1, 
R.drawable.sample_2, R.drawable.sample_3, 
R.drawable.sample_4, R.drawable.sample_5, 
R.drawable.sample_6, R.drawable.sample_7, 
R.drawable.sample_0, R.drawable.sample_1, 
R.drawable.sample_2, R.drawable.sample_3, 
R.drawable.sample_4, R.drawable.sample_5, 
R.drawable.sample_6, R.drawable.sample_7 
}; 

2 个答案:

答案 0 :(得分:4)

将图像放入SD卡中的[图像]文件夹

public class ChannelImageAdapter extends BaseAdapter {

    int mGalleryItemBackground;
    private Context mContext;
    File[] images;
    File[] files;
    public ChannelImageAdapter(Context c, int folderID) {
        mContext = c;

        File dir = new File(Environment.getExternalStorageDirectory() + "/images");
        files = dir.listFiles();
        images = files[folderID].listFiles();

    }
    public int getCount() {
        return images.length;
    }
    public Object getItem(int position) {
        return images[position].getAbsolutePath();
    }
    public long getItemId(int position) {
        return position;
    }
    public String getAlbumName(int folderID) {
        return files[folderID].getName();
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;
        //Bitmap bm = BitmapFactory
        //      .decodeFile(images[position].getAbsolutePath());
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 10, 5, 10);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(Bitmap.decode(images[position].getAbsoluteFile()));
        return imageView;

    }


}

答案 1 :(得分:0)

public class ChannelImageAdapter extends BaseAdapter {

    int mGalleryItemBackground;
    private Context mContext;
    File[] images;
    File[] files;
    public ChannelImageAdapter(Context c, int folderID) {
        mContext = c;

        File dir = new File(Environment.getExternalStorageDirectory() + "/images");
        files = dir.listFiles();
        images = files[folderID].listFiles();

    }
    public int getCount() {
        return images.length;
    }
    public Object getItem(int position) {
        return images[position].getAbsolutePath();
    }
    public long getItemId(int position) {
        return position;
    }
    public String getAlbumName(int folderID) {
        return files[folderID].getName();
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 10, 5, 10);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(Bitmap.decode(images[position].getAbsoluteFile()));
        return imageView;

    }
}