I had developed an application in which I have to display images from specific folder in sd card. My code is working fine but it is showing all images that are sd card. I had given selection args as my folder's name as it has been metioned the following post:
Displaying images from a specific folder on the SDCard using a gridview
But, it didn't worked. Please, suggest me how to do this:
Below I am posting my code.
public class Album1Activity extends Activity {
static Cursor imageCursor;
static int columnIndex;
GridView gridView;
Intent in;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
String[] imgColumnID = {MediaStore.Images.Thumbnails._ID};
Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
imageCursor = managedQuery(uri, imgColumnID, null, null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getApplicationContext()));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String[] dataLocation = {MediaStore.Images.Media.DATA};
//String []dataLocation = {Environment.getExternalStorageDirectory().getAbsolutePath()};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
dataLocation, null, null, null);
columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imageCursor.moveToPosition(position);
String imgPath = imageCursor.getString(columnIndex);
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", imgPath);
startActivity(in);
}//onItemClickListener
});
}//onCreate
public static class ImageAdapter extends BaseAdapter{
private Context context;
//Album1Activity act = new Album1Activity();
public ImageAdapter(Context context){
this.context = context;
}//ImageAdapter
public int getCount() {
// TODO Auto-generated method stub
return imageCursor.getCount();
}//getCount
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}//getItem
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}//getItemId
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if(convertView == null){
iv = new ImageView(context);
imageCursor.moveToPosition(position);
int imageID = imageCursor.getInt(columnIndex);
iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
"" +imageID));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setPadding(5, 5, 5, 5);
iv.setLayoutParams(new GridView.LayoutParams(100, 100));
}//if
else{
iv = (ImageView)convertView;
}//else
return iv;
}//getView
}//ImageAdapter
}//Album1Activity
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…