开发者

how to dynamically display the single image from the sdcard

I had done the program the display all the images from the sdcard dynamically. But now ,I want to display single image dynamically from the sdcard instead of display all images . my coding is as follows

public class Gallery1Activity extends Activity {
// private ArrayList<String> imglist;


/** Called when the activity is first created. */开发者_如何学运维
@Override
public void onCreate(Bundle savedInstanceState) {

    ArrayList arr = new ArrayList();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallerygrid);

    GridView gv1=(GridView) this.findViewById(R.id.gridView1);
    //gv1.setAdapter(new galleryImageAdapter(this));

    arr = galldatabase();
    gv1.setAdapter(new galleryImageAdapter(this,arr));

}

private ArrayList galldatabase() {
    // TODO Auto-generated method stub
    ArrayList ThumbsIDList = new ArrayList();
    //Uri u=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;

    /*String[] projection =new String[]{
        Images.Thumbnails._ID,
        Images.Thumbnails.DATA,
        Images.Thumbnails.IMAGE_ID};*/

    Cursor galleryimagecursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,new String[]{
            Images.Thumbnails._ID,

            Images.Thumbnails.DATA} , null, null, null);

    if(galleryimagecursor!=null&&galleryimagecursor.moveToFirst()){


        String thumbsID;
        String thumbsImageID;
        String thumbsData;



         int num=0;

        do{


            thumbsID=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID));
            thumbsData=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails.DATA));

            Log.i("BMP","size "+thumbsID+" "+thumbsData);  


            num++;

            /*if(thumbsImageID!= null) {*/
                ThumbsIDList.add(thumbsID);
                /*ThumbsImageIDList.add(galleryimagecursor.getString(thumbsImageIDcol));
                ThumbsDataList.add(galleryimagecursor.getString(thumbsDataCol));
        }*/



        }
        while(galleryimagecursor.moveToNext());


    }
    return ThumbsIDList;

}

}

then the adapter code follows

public class galleryImageAdapter extends BaseAdapter {

Context con;
private ArrayList<String> imgList;

private String thumbsID;
public galleryImageAdapter(Context c,ArrayList arr){
    con=c;
    imgList = arr;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imgList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View v;

    if( convertView==null){

        LayoutInflater li;
        li = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=li.inflate(R.layout.galleryadapter,null);

        final ImageView iv1=(ImageView)v.findViewById(R.id.galimage);
        TextView tv1=(TextView)v.findViewById(R.id.galimagtext);
        tv1.setText("Image"+position);
        Log.d("imagevalue",imgList.get(position));



        iv1.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imgList.get(position)/*galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID)*//*imgList.get(position)*/)); 




    }
    else 
        v=convertView;

    return v;
}

}


you have arraylist of thumb id of images using arr = galldatabase(); create another arraylist (newarr) which will have only 1 element. if you want to show 2nd image just copy thumb id of that image from arr arr and store it in new array list

example

ArrayList newarr = new ArrayList();
newarr.add(arr.get(random position));

Assign this list to adapter instead of assigning to original array list which contains list of all image

use

gv1.setAdapter(new galleryImageAdapter(this,newarr));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜