package com.camera.simplemjpeg;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class image_miniature extends Activity 
{
	  
    List <String> ImageList;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_miniature);
        
        /*** Get Images from SDCard ***/
        ImageList = getSD();
        
        // listView1
        final ListView LView1 = (ListView)findViewById(R.id.listView1); 
        	
        LView1.setAdapter(new ImageAdapter(this,ImageList));
               
        final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        
        LView1.setOnItemClickListener(new OnItemClickListener() 
        {
			public void onItemClick(AdapterView<?> parent, View v,	int position, long id) 
			{

				String strPath = ImageList.get(position).toString();
                
                String name = new File(strPath).getName();
                
                Toast.makeText(image_miniature.this, name , Toast.LENGTH_LONG).show();
                
              
 		    	
			}
		});
    
      
    }

    private List <String> getSD()
    {
    	
	    List <String> it = new ArrayList <String>();
	    File f = new File ("/mnt/sdcard");
	    File[] files = f.listFiles();
	    
	    for (int i = 0; i <files.length; i++)
	    {
	    	File  file = files[i];
	    	Log.d("Count",file.getPath());
	    	
	    	if (file.isFile()) 
	    	{
	    		    	String extension = file.getPath().substring(file.getPath().lastIndexOf('.') + 1);
	    		    	if(extension.equals("jpg"))
	    		    	{
	    		    		it.add (file.getPath());
	    		    	}
	    	} 
	    }
	    return it;
    	/*
    	 List <String> it = new ArrayList <String>();
 	    File f = new File ("/mnt/sdcard");
 	    File[] files = f.listFiles ();
 	    
 	    for (int i = 0; i <files.length; i++)
 	    {
 	    	File  file = files[i];
 	    	Log.d("Count",file.getPath());
 		    it.add (file.getPath());
 	    }
 	    return it;
    	*/
    }
    
    public class ImageAdapter extends BaseAdapter 
    {
        private Context context;
        private List <String> lis;
        
        public ImageAdapter(Context c, List <String> li) 
        {
        	// TODO Auto-generated method stub
            context = c;
            lis = li;
        }
 
        public int getCount() {
        	// TODO Auto-generated method stub
        	int n = 10;
            return n;
        	//return lis.size();
        }
 
        public Object getItem(int position) {
        	// TODO Auto-generated method stub
            return position;
        }
 
        public long getItemId(int position) {
        	// TODO Auto-generated method stub
            return position;
        }
        
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.image_miniature_colonne, null); 
			}
					
			// ColImage
			String strPath = lis.get(position).toString();
			Bitmap bm = BitmapFactory.decodeFile(strPath);

			ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImage);
			imageView.getLayoutParams().height = 100;
			imageView.getLayoutParams().width = 100;
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setImageBitmap(bm);
			
			// ColPosition
			TextView txtPosition = (TextView) convertView.findViewById(R.id.ColPosition);
			txtPosition.setPadding(10, 0, 0, 0);
			txtPosition.setText("Position : " + position);
			
			// ColPicname
			TextView txtPicName = (TextView) convertView.findViewById(R.id.ColPicname);
			txtPicName.setPadding(50, 0, 0, 0);
			String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length());
			txtPicName.setText("Pic : " + fileName);

		 
			return convertView;
				
		}

    } 
    
   
}