Thursday, 22 March 2012

Complete code display image from specific folder in SD Card in grid view

This is the My XML..
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:verticalSpacing="10dp"
    android:background="@drawable/shape"
    android:horizontalSpacing="100dp"
    android:numColumns="auto_fit"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
  
    />

Source code



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

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
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.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.nexbits.digitechi.HeaderAndButtonActivity;
import com.nexbits.digitechi.R;

public class ViewImageAdapter extends HeaderAndButtonActivity {
    List<String> tFileList;
    public Bitmap bm;
    ByteArrayOutputStream baos;
    ImageView jpgView;
    File f;
    File[] files;

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

        GridView g = (GridView) findViewById(R.id.myGrid);
        //g.setAdapter(new ImageAdapter(this, ReadSDCard()));
       
        registerForContextMenu(g);
       
       

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
               
   
               
               
                Toast.makeText(ViewImageAdapter.this, "" + position,
                        Toast.LENGTH_SHORT).show();
                System.out.println("Inside Onclick...............+position");

                //setContentView(R.layout.full_image);
                //ImageView imageView = (ImageView) findViewById(R.id.full_image_view);

    System.out.println("inside the full image?????????????????????????????????");


                 Intent i = new Intent(ViewImageAdapter.this, imageViewFlipper.class);
                // passing array index
                // i.putExtra("id", position);
                 // startActivity(i);
                System.out
                        .println("Inside the intent////////////////////////////////////");
                bm = BitmapFactory.decodeFile(files[position].getPath());
                //imageView.setImageBitmap(bm);
                System.out
                        .println("Inside ihe image view///////////////////????????????????????????");
            }
        });
        registerForContextMenu(g);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
       
        if (v.getId() == R.id.myGrid) {
            System.out.println("Inside the id.list//////////////////////////");
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
             //menu.setHeaderTitle(mThumbIds[info.position]);
            String[] menuItems = getResources().getStringArray(R.array.menu);
            for (int i = 0; i < menuItems.length; i++) {
                menu.add(Menu.NONE, i, i, menuItems[i]);

            }
        }
       

        // menu.add(R.string.gallery_2_text);
    }


    private List<String> ReadSDCard() {
        tFileList = new ArrayList<String>();

        // It have to be matched with the directory in SDCard
        f = new File("/data/digitechimages");// Here you take your specific folder//

        files = f.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
            }
        });

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            /*
             * It's assumed that all file in the path are in supported type
             */
            tFileList.add(file.getPath());
        }
        return tFileList;
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;
        private List<String> FileList;

        public ImageAdapter(Context c, List<String> fList) {
            mContext = c;
            FileList = fList;
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return FileList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                Bitmap bm = BitmapFactory.decodeFile(FileList.get(position)
                        .toString());
                imageView.setImageBitmap(bm);
                // imageView.setImageResource(FileList[position]);
                imageView.setLayoutParams(new GridView.LayoutParams(175, 175));
                imageView.setAdjustViewBounds(true);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 5, 8);
                imageView.setBackgroundResource(mGalleryItemBackground);

            } else {
                imageView = (ImageView) convertView;
            }

            // imageView.setImageResource(fi[position]);

            return imageView;
        }

    }

}

How to use Flipper in GridView in android

Firstly You create a Project from  in android any name
Now You Create Grid Xml...

<?xml version="1.0" encoding="utf-8"?>


<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
   
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="80px"
    android:stretchMode="columnWidth"
   
    android:gravity="center"
    />
Now create another Xml...


<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >       
        <ImageView
            android:id="@+id/zero"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="matrix"
            android:src="@drawable/wait"
        />
    </FrameLayout>
   
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ImageView
            android:id="@+id/one"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="matrix"
            android:src="@drawable/wait"
        />
    </FrameLayout>
   
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ImageView
            android:id="@+id/two"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="matrix"
            android:src="@drawable/wait"
        />
    </FrameLayout>
   
</ViewFlipper>

Now added the source code

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.buuuk.android.util.FileUtils;


public class ImageDemoActivity extends Activity {

    GridView mGrid;
    private static final String DIRECTORY = "/sdcard/";
    private static final String DATA_DIRECTORY = "/sdcard/.ImageDemoActivity/";
    private static final String DATA_FILE = "/sdcard.ImageDemoActivity/imagelist.dat";
    List<String> ImageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps(); // do this in onresume?

        setContentView(R.layout.grid);
        mGrid = (GridView) findViewById(R.id.myGrid);
        //mGrid.setOnScrollListener(this);
      
        System.out.println("After the mgrid>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        File data_directory = new File(DATA_DIRECTORY);
        if (!data_directory.exists()) {
            if (data_directory.mkdir()) {
                FileUtils savedata = new FileUtils();
                Toast toast = Toast.makeText(ImageDemoActivity.this,
                        "Please wait while we search your SD Card for images...", Toast.LENGTH_SHORT);
                toast.show();
                SystemClock.sleep(100);
                ImageList = FindFiles();
                savedata.saveArray(DATA_FILE, ImageList);
               
            } else {
                ImageList = FindFiles();
            }

        }
        else {
            File data_file= new File(DATA_FILE);
            if (!data_file.exists()) {
                FileUtils savedata = new FileUtils();
                Toast toast = Toast.makeText(ImageDemoActivity.this,
                        "Please wait while we search your SD Card for images...", Toast.LENGTH_SHORT);
                toast.show();
                SystemClock.sleep(100);
                ImageList = FindFiles();
                savedata.saveArray(DATA_FILE, ImageList);
            } else {
                FileUtils readdata = new FileUtils();
                ImageList = readdata.loadArray(DATA_FILE);
            }
        }
        mAdapter = new AppsAdapter();
         mGrid.setAdapter(mAdapter);
         mThumbnails = new HashMap<Integer,SoftReference<ImageView>>();
         mThumbnailImages = new HashMap<Integer,SoftReference<Bitmap>>();
    }
   
    private List<String> FindFiles() {
        final List<String> tFileList = new ArrayList<String>();
        Resources resources = getResources();
        // array of valid image file extensions
        String[] imageTypes = resources.getStringArray(R.array.image);
        FilenameFilter[] filter = new FilenameFilter[imageTypes.length];

        int i = 0;
        for (final String type : imageTypes) {
            filter[i] = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith("." + type);
                }
            };
            i++;
        }

        FileUtils fileUtils = new FileUtils();
        File[] allMatchingFiles = fileUtils.listFilesAsArray(
                new File(DIRECTORY), filter, -1);
        for (File f : allMatchingFiles) {
            tFileList.add(f.getAbsolutePath());
        }
        return tFileList;
    }

    private List<ResolveInfo> mApps;

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public AppsAdapter mAdapter;
    public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
            map = new HashMap();
        }
       
        public Map<Integer,SoftReference<Bitmap>> map;
        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(ImageDemoActivity.this);
                i.setScaleType(ImageView.ScaleType.FIT_XY);
                i.setLayoutParams(new GridView.LayoutParams(80, 80));
            } else {
                i = (ImageView) convertView;
            }
           
            if(!mBusy && mThumbnailImages.containsKey(position)
                    && mThumbnailImages.get(position).get()!=null) {
                i.setImageBitmap(mThumbnailImages.get(position).get());
            }
            else  {
                i.setImageBitmap(null);
                if(!mBusy)loadThumbnail(i,position);
            }
           
            i.setOnClickListener(new OnClickListener() {
               
                @Override
                public void onClick(View v) {
                    Toast.makeText(ImageDemoActivity.this, "Opening Image...", Toast.LENGTH_LONG).show();

                    // TODO Auto-generated method stub
                    SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
                            MODE_PRIVATE);
                   
                    SharedPreferences.Editor indexEditor = indexPrefs.edit();
                    indexEditor.putInt("currentIndex", position);
                    indexEditor.commit();
                    System.out.println("<<<<<<<<<<<<<Before the intent>>>>>>>>>>>>>>>>>>>>>>");
                    final Intent intent = new Intent(ImageDemoActivity.this, ImageViewFlipper.class);
                    startActivity(intent);
                   System.out.println("<<<<<<<<<<<<<<<<After the Intent>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                }
            });
           
           
            return i;
        }


        public final int getCount() {
            return ImageList.size();
        }

        public final Object getItem(int position) {
            return ImageList.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
       
    }
   
    public boolean mBusy = false;
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            mBusy = false;
            mAdapter.notifyDataSetChanged();
            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            mBusy = true;
            // mStatus.setText("Touch scroll");
            break;
        case OnScrollListener.SCROLL_STATE_FLING:
            mBusy = true;
            // mStatus.setText("Fling");
            break;
        }
    }
   
    private Map<Integer,SoftReference<ImageView>> mThumbnails;
    private Map<Integer,SoftReference<Bitmap>> mThumbnailImages;

    private void loadThumbnail( ImageView iv, int position ){
        mThumbnails.put(position,new SoftReference<ImageView>(iv));
        try{new LoadThumbnailTask().execute(position);}catch(Exception e){}
    }
    public void onThumbnailLoaded( int position, Bitmap bm, LoadThumbnailTask t ){
        Bitmap tn = bm;
        if( mThumbnails.get(position).get() != null && tn!=null)
            mThumbnails.get(position).get().setImageBitmap(tn);
       
        t.cancel(true);
    }
   
    public class LoadThumbnailTask extends AsyncTask<Integer, Void, Bitmap>{
        private int position;
        @Override
        protected Bitmap doInBackground(Integer... params) {
            try{
                position = params[0];
                Bitmap bitmapOrg = BitmapFactory.decodeFile(ImageList.get(position));
           
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
        
                //new width / height
                int newWidth = 80;
                int newHeight = 80;
   
                // calculate the scale
                float scaleWidth = (float) newWidth / width;
                float scaleHeight = (float) newHeight/ (height * scaleWidth) ;
                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
   
                // resize the bit map
                matrix.postScale(scaleWidth, scaleWidth);
                matrix.postScale(scaleHeight, scaleHeight);
   
                // recreate the new Bitmap and set it back
                Bitmap bm = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
               
                mThumbnailImages.put(position, new SoftReference<Bitmap>(bm));
                System.gc();
                return bm;
            }catch(Exception e){
               
            }
           
           
            return null;
        }
        protected void onPostExecute(Bitmap bm) {
            
             onThumbnailLoaded(position, bm, this);
         }
       
    }

}
 Now take another class for flipper

public class ImageViewFlipper extends TouchActivity {
   
    private static final int EXIT = 0;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private static final String DIRECTORY = "/data/digitechimages/";//Here You take your path//
    private static final String DATA_DIRECTORY = "/data/digitechimages/";//Here You take your path//
    private static final String DATA_FILE = "/data/digitechimages/imagelist.dat";//Here You take your path//
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private int currentView = 0;
    List<String> tFileList;
    private int currentIndex = 0;
    private int maxIndex = 0;
    private float mMinZoomScale=1;

    FileOutputStream output = null;
    OutputStreamWriter writer = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        setContentView(R.layout.slide);
        ImageView iv = (ImageView) findViewById(R.id.zero);
        /*iv.setOnTouchListener(this);
        findViewById(R.id.one).setOnTouchListener(this);
        findViewById(R.id.two).setOnTouchListener(this);*/

        File data_directory = new File(DATA_DIRECTORY);
        if (!data_directory.exists()) {
            if (data_directory.mkdir()) {
                FileUtils savedata = new FileUtils();
                Toast toast = Toast.makeText(ImageViewFlipper.this,
                        "Please wait while we search your SD Card for images...", Toast.LENGTH_SHORT);
                toast.show();
                SystemClock.sleep(100);
                tFileList = FindFiles();
                savedata.saveArray(DATA_FILE, tFileList);
               
            } else {
                tFileList = FindFiles();
            }

        }
        else {
            File data_file= new File(DATA_FILE);
            if (!data_file.exists()) {
                FileUtils savedata = new FileUtils();
                Toast toast = Toast.makeText(ImageViewFlipper.this,
                        "Please wait while we search your SD Card for images...", Toast.LENGTH_SHORT);
                toast.show();
                SystemClock.sleep(100);
                tFileList = FindFiles();
                savedata.saveArray(DATA_FILE, tFileList);
            } else {
                FileUtils readdata = new FileUtils();
                tFileList = readdata.loadArray(DATA_FILE);
            }
        }
       
        if (tFileList == null) {
            quit();
        }
       
        SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
                MODE_PRIVATE);
        if (indexPrefs.contains("currentIndex")) {
            currentIndex = indexPrefs.getInt("currentIndex", 0);
        }
       
        maxIndex = tFileList.size() - 1;
       
        Log.v("currentIndex", "Index: "+currentIndex);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);

        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        Drawable d = Drawable.createFromPath(tFileList
                .get(currentIndex));
        iv.setImageDrawable(d);
        resetImage(iv,d);
        System.gc();
       
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
       
        int NONE = Menu.NONE;
        menu.add(NONE, EXIT, NONE, "Exit");
        return true;
    }
   
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case EXIT:
            quit();
            break;
        }

        return super.onOptionsItemSelected(item);
    }
   
    protected void onPause() {
        super.onPause();

        SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
                MODE_PRIVATE);
       
        SharedPreferences.Editor indexEditor = indexPrefs.edit();
        indexEditor.putInt("currentIndex", currentIndex);
        indexEditor.commit();
    }
   
    protected void onResume() {
        super.onResume();
        SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
                MODE_PRIVATE);
        if (indexPrefs.contains("currentIndex")) {
            currentIndex = indexPrefs.getInt("currentIndex", 0);
        }   
    }

    private List<String> FindFiles() {
        final List<String> tFileList = new ArrayList<String>();
        Resources resources = getResources();
        // array of valid image file extensions
        String[] imageTypes = resources.getStringArray(R.array.image);
        FilenameFilter[] filter = new FilenameFilter[imageTypes.length];

        int i = 0;
        for (final String type : imageTypes) {
            filter[i] = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith("." + type);
                }
            };
            i++;
        }

        FileUtils fileUtils = new FileUtils();
        File[] allMatchingFiles = fileUtils.listFilesAsArray(
                new File(DIRECTORY), filter, -1);
        for (File f : allMatchingFiles) {
            tFileList.add(f.getAbsolutePath());
        }
        return tFileList;
    }

   

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(final MotionEvent e){
           
           
            ImageView view = (ImageView)findViewById(R.id.zero);
            switch(currentView){
            case 0: view = (ImageView)findViewById(R.id.zero); break;
            case 1: view = (ImageView)findViewById(R.id.one); break;
            case 2:view = (ImageView)findViewById(R.id.two); break;               
            }
           
            resetImage(view,view.getDrawable());
            return true;
        }
       
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);

                    if (currentIndex == maxIndex) {
                        currentIndex = 0;
                    } else {
                        currentIndex = currentIndex + 1;
                    }
                    ImageView iv;
                    Drawable d = Drawable.createFromPath(tFileList
                            .get(currentIndex));
                   
                    if (currentView == 0) {
                        currentView = 1;
                         iv = (ImageView) findViewById(R.id.one);

                        iv.setImageDrawable(d);
                       
                       
                        System.gc();
                    } else if (currentView == 1) {
                        currentView = 2;
                         iv = (ImageView) findViewById(R.id.two);
                       
                        iv.setImageDrawable(d);
                        System.gc();
                    } else {
                        currentView = 0;
                         iv = (ImageView) findViewById(R.id.zero);

                        iv.setImageDrawable(d);
                        System.gc();
                    }
                    resetImage(iv,d);
                    Log.v("ImageViewFlipper", "Current View: " + currentView);
                    viewFlipper.showNext();
               
                    return true;
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                   
                   
                   
                    if (currentIndex == 0) {
                        currentIndex = maxIndex;
                    } else {
                        currentIndex = currentIndex - 1;
                    }
                    ImageView iv;
                    Drawable d = Drawable.createFromPath(tFileList
                            .get(currentIndex));
                    if (currentView == 0) {
                        currentView = 2;
                        iv = (ImageView) findViewById(R.id.two);
                        iv.setImageDrawable(d);
                        System.gc();
                    } else if (currentView == 2) {
                        currentView = 1;
                        iv = (ImageView) findViewById(R.id.one);
                        iv.setImageDrawable(d);
                        System.gc();
                    } else {
                        currentView = 0;
                        iv = (ImageView) findViewById(R.id.zero);
                        iv.setImageDrawable(d);
                        System.gc();
                    }
                    resetImage(iv,d);
                    Log.v("ImageViewFlipper", "Current View: " + currentView);
                    viewFlipper.showPrevious();
                    return true;
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
       
    }
    @Override
    public void resetImage(ImageView iv, Drawable draw) {
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
       
        int orientation = 0;
        if( rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
            orientation = 0;
        else
            orientation = 1;
       
        matrix = new Matrix();
        matrix.setTranslate(1f, 1f);
        float scale = 1;
       
        mMinZoomScale = 1;
        if( orientation==0
                //&& (float)draw.getIntrinsicWidth() >  (float)getWindowManager().getDefaultDisplay().getWidth()
                ) {
           
            scale = (float)getWindowManager().getDefaultDisplay().getWidth()/(float)draw.getIntrinsicWidth(); 
            mMinZoomScale = scale;
            matrix.postScale(scale,scale);
       
            iv.setImageMatrix(matrix);
        }else if( orientation==1
                //&& (float)draw.getIntrinsicHeight() >  (float)getWindowManager().getDefaultDisplay().getHeight()
                ){
            scale = (float)getWindowManager().getDefaultDisplay().getHeight()/(float)draw.getIntrinsicHeight(); 
            mMinZoomScale = scale;
            matrix.postScale(scale,scale);
       
            iv.setImageMatrix(matrix);
        }
           
           
        float transX = (float) getWindowManager().getDefaultDisplay().getWidth()/2
                                - (float)(draw.getIntrinsicWidth()*scale)/2
                                ;
           
        float transY = (float)
                getWindowManager().getDefaultDisplay().getHeight()/2
                                    - (float)(draw.getIntrinsicHeight()*scale)/2
                                   
                                    ;
        matrix.postTranslate(transX,transY);
        iv.setImageMatrix(matrix);
    }
   
   
    @Override
    public float getMinZoomScale(){
        return mMinZoomScale;
    }
   
    @Override
    public boolean onTouchEvent(MotionEvent rawEvent) {
        if(gestureDetector.onTouchEvent(rawEvent))
            return true;
           
       
        ImageView view = (ImageView)findViewById(R.id.zero);
        switch(currentView){
        case 0: view = (ImageView)findViewById(R.id.zero); break;
        case 1: view = (ImageView)findViewById(R.id.one); break;
        case 2:view = (ImageView)findViewById(R.id.two); break;               
        }   
        onTouchEvented(view, rawEvent);
       
        return true;
    }
   

    public void quit() {
        SharedPreferences indexPrefs = getSharedPreferences("currentIndex",
                MODE_PRIVATE);
       
        SharedPreferences.Editor indexEditor = indexPrefs.edit();
        indexEditor.putInt("currentIndex", 0);
        indexEditor.commit();
       
        File settings = new File(DATA_FILE);
        settings.delete();
        finish();
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
        System.exit(0);
    }
}