简体   繁体   中英

Images from SD-card to GridView

I'm looking for some information about how to read images from the phones SD-card and then place them in a GridView and finally be able to click and select one of the images and display it in full size to begin with.

I'm looking for a tutorial or example that is easy to follow and understand. I have searched, but I find it hard to find a tutorial like this. Perhaps it's because I don't know the right key words. I have followed the GridView example on the Android Developer webpage, but I'm looking for a continuation.

So far I have learned that I need to work with MediaStore content providers, querys and cursors.

I would preciate if someone could give me some more info or direction to get going. Thanks!

Here is a simple example to add a imageview into a grid view.

@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(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}

public class ImageAdapter extends BaseAdapter 
{
    private Context context;

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

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

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

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(...);
        return imageView;
    }
}    

Here is how to put a image file into a imageview.

Solution1:

ImageView i = new ImageView(mContext);  

Bitmap bm = BitmapFactory.decodeFile(...);  
i.setImageBitmap(bm);  

i.setLayoutParams(new Gallery.LayoutParams(150, 100));  
i.setScaleType(ImageView.ScaleType.FIT_XY);  
i.setBackgroundResource(...); 

Solution2:

ImageView im = new ImageView(mContext);   
im.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+id)); 

Here is a beginner and good tutorial on grid view. In that tutorial the images are placed in the drawable folder. since you want to read the images from the memory card, just find the path for those images and supply them to the imageView's in the gridview. let me know if you need help with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM