I had developed an application where I want to display images inside grid view from specific folder of sd card. The application is working but only 1st image from folder is appearing in every grid, while I want all images should display. I am not getting where I went wrong. Below, I am posting my code:
Album Activity:
public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
GridView gridView;
ImageAdapter adapter;
Intent in;
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
prepareList();
adapter = new ImageAdapter(this, mediaFiles);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", position);
startActivity(in);
}
});
}//onCreate
public static Bitmap prepareList() {
imageDir = new File(Environment.getExternalStorageDirectory().toString()+
"/diplomat");
mediaFiles = imageDir.listFiles();
Bitmap bmp = null;
for(File imagePath:mediaFiles){
try{
bmp = BitmapFactory.decodeStream(imagePath.toURL().openStream());
}catch(Exception e){
Log.d(TAG, "Exception: "+e.toString());
}//catch
}//for
Log.d(TAG, "prepareList() called");
return bmp;
}//prepareList
}//class
Image Adapter:
public class ImageAdapter extends BaseAdapter{
Activity act;
File[] mFiles;
public static final String TAG = "ImageAdapter";
public ImageAdapter(Activity act, File[] mFiles){
super();
this.act = act;
this.mFiles = mFiles;
}//ImageAdapter
public int getCount() {
return mFiles.length;
}//getCount
public Object getItem(int postion) {
return mFiles[postion];
}//getItem
public long getItemId(int position) {
return 0;
}//getItemId
public static class ViewHolder{
ImageView iv;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater li = act.getLayoutInflater();
if(convertView == null){
view = new ViewHolder();
convertView = li.inflate(R.layout.gridview_row, null);
view.iv = (ImageView)convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}//if
else{
view = (ViewHolder)convertView.getTag();
}//else
Bitmap bmp = Album3Activity.prepareList();
view.iv.setImageBitmap(bmp);
Log.d(TAG, "getView called");
return convertView;
}//getView
}//ImageAdapter