Skip to content Skip to sidebar Skip to footer

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 di

Solution 1:

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

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridViewgridView= (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(newImageAdapter(this));

    gridView.setOnItemClickListener(newOnItemClickListener() 
    {
        publicvoidonItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}

publicclassImageAdapterextendsBaseAdapter 
{
    private Context context;

    publicImageAdapter(Context c) 
    {
        context = c;
    }

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

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

    publiclonggetItemId(int position) {
        return position;
    }

    //---returns an ImageView view---public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = newImageView(context);
            imageView.setLayoutParams(newGridView.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:

ImageViewi=newImageView(mContext);  

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

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

Solution2:

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

Solution 2:

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.

Post a Comment for "Images From Sd-card To Gridview"