Gallery to display all Images stored in sdcard, but it fails

All your problems with Audio, Video and Images.

Gallery to display all Images stored in sdcard, but it fails

Postby griffinshi » Fri Mar 26, 2010 10:21 am

Hi all.

I use only one View - Gallery to display all Images stored in sdcard, by MediaStorte....

but it seems some error that no Images..

And now, i release all source code, pls help me !


//HiGalleryTest

public class HiGalleryTest extends Activity {
Gallery gallery;

ImagesAdapter iAdapter;

Cursor cursor;

Activity activity;

int column_index;

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

gallery = (Gallery)findViewById(R.id.gallery);

String [] proj={MediaStore.Images.ImageColumns._ID};

cursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
proj,
null,
null,
null);
column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

iAdapter = new ImagesAdapter(this);

gallery.setAdapter(iAdapter);

}

public class ImagesAdapter extends BaseAdapter {
Activity activity;

ImagesAdapter(Activity a){
activity = a;
}

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

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

@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
ImageView image;
image = new ImageView(activity);

if(convertView == null){
cursor.moveToPosition(position);
int id = cursor.getInt(column_index);
image.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+id));
}
else {
image =(ImageView)convertView;

image.setImageResource(R.drawable.icon);
}

return image;
}

}



}


it's all! thanks
griffinshi
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Feb 26, 2010 8:53 am
Location: cn

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 24 guests