001.
public
class
MainActivity
extends
Activity {
002.
003.
List <String> ImageList;
004.
005.
@Override
006.
public
void
onCreate(Bundle savedInstanceState) {
007.
super
.onCreate(savedInstanceState);
008.
setContentView(R.layout.activity_main);
009.
010.
011.
ImageList = getSD();
012.
013.
014.
final
GridView gView1 = (GridView)findViewById(R.id.gridView1);
015.
016.
gView1.setAdapter(
new
ImageAdapter(
this
,ImageList));
017.
018.
019.
020.
gView1.setOnItemClickListener(
new
OnItemClickListener() {
021.
public
void
onItemClick(AdapterView<?> parent, View v,
022.
int
position,
long
id) {
023.
024.
View layout = inflater.inflate(R.layout.custom_full_imagedialog,
025.
(ViewGroup) findViewById(R.id.layout_root));
026.
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
027.
Toast.makeText(getApplicationContext(),
028.
"Your selected : "
+ ImageList.get(position).toString(), Toast.LENGTH_SHORT).show();
029.
030.
}
031.
});
032.
033.
}
034.
035.
private
List <String> getSD()
036.
{
037.
List <String> it =
new
ArrayList <String>();
038.
File f =
new
File(
039.
android.os.Environment.getExternalStorageDirectory()
040.
+ File.separator + Constance.PHOTO_ALBUM);
041.
File[] files = f.listFiles ();
042.
043.
044.
for
(
int
i =
0
; i <files.length; i++)
045.
{
046.
File file = files[i];
047.
Log.d(
"Count"
,file.getPath());
048.
it.add (file.getPath());
049.
}
050.
return
it;
051.
}
052.
053.
public
class
ImageAdapter
extends
BaseAdapter
054.
{
055.
private
Context context;
056.
private
List <String> lis;
057.
058.
public
ImageAdapter(Context c, List <String> li)
059.
{
060.
061.
context = c;
062.
lis = li;
063.
}
064.
065.
066.
067.
public
int
getCount() {
068.
069.
return
lis.size();
070.
}
071.
072.
public
Object getItem(
int
position) {
073.
074.
return
position;
075.
}
076.
077.
public
long
getItemId(
int
position) {
078.
079.
return
position;
080.
}
081.
082.
public
View getView(
int
position, View convertView, ViewGroup parent) {
083.
084.
085.
LayoutInflater inflater = (LayoutInflater) context
086.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
087.
088.
089.
if
(convertView ==
null
) {
090.
convertView = inflater.inflate(R.layout.show_image,
null
);
091.
}
092.
093.
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
094.
String strPath = lis.get(position).toString();
095.
096.
097.
String fileName = strPath.substring( strPath.lastIndexOf(
'/'
)+
1
, strPath.length() );
098.
textView.setText(fileName);
099.
100.
101.
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
102.
Bitmap bm = BitmapFactory.decodeFile(strPath);
103.
imageView.setImageBitmap(bm);
104.
105.
return
convertView;
106.
107.
}