Nitish
Nitish

Reputation: 3155

Same images are appearing multiple times in grid view

I had developed an album app in which I am using grid View. The app is working but every time when I press back button and return to my app, one more copy of image is appearing in the grid view. For instance, I have 7 images in the grid view, when I press back button and then return to app, it becomes 14. Next time total images will be 21 and so on. I am reading images from SD card. My app is having full screen functionality, so same thing occurs that case as well. I am not getting why it happened. Below, I am posting my code:

Album Activity

public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
static String name = null;
static ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
static ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    //Log.d("Lenght of images",""+mediaFiles.length);
    for(File file : mediaFiles)
    {
        bmpArray.add(convertToBitmap(file));
        fileName.add(readFileName(file));
        Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
        Log.d(TAG, "call to convertToBitmap");
    }//for

    adapter = new ImageAdapter(this, bmpArray, fileName);
    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 convertToBitmap(File file) 
{
    URL url = null;
    try 
    {
        url = file.toURL();
    } catch (MalformedURLException e1) 
    {
        Log.d(TAG, e1.toString());
    }//catch

    Bitmap bmp = null;
    try
    {
       bmp = BitmapFactory.decodeStream(url.openStream());
       //bmp.recycle();
    }catch(Exception e)
    {
        Log.d(TAG, "Exception: "+e.toString());
    }//catch
    return bmp;
}//convertToBitmap

public String readFileName(File file){
    String name = file.getName();
    return name;
}//readFileName
}//class

Image Adapter

public class ImageAdapter extends BaseAdapter {
Activity act;
ArrayList<Bitmap> mFiles;
static ArrayList<String> mName;
public static final String TAG = "ImageAdapter";
static String name = null;
public ImageAdapter(Activity act, ArrayList<Bitmap> mFiles, ArrayList<String>     mName) {
    super();
    this.act = act;
    this.mFiles = mFiles;
    ImageAdapter.mName = mName;
    Log.d("mfiles", "" + this.mFiles.size());
}// ImageAdapter

public int getCount() {
    return mFiles.size();
}// getCount

public Object getItem(int postion) {
    return mFiles.get(postion);
}// getItem

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

public static class ViewHolder {
    ImageView iv;
    TextView tv;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    LayoutInflater li = act.getLayoutInflater();
    if (convertView == null) {
        Log.d("tag", "null");
        view = new ViewHolder();
        convertView = li.inflate(R.layout.gridview_row, null);
        view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
        view.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(view);
    }// if
    else {
        view = (ViewHolder) convertView.getTag();
    }// else
    view.iv.setImageBitmap(mFiles.get(position));
    view.tv.setText(readName(position));
    Log.d(TAG, "getView method called");
    return convertView;
}// getView

public static String  readName(int position) {
    String name = mName.get(position);
    return name;
}//readName

    }// ImageAdapter

FullScreen.java

public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
ImageView imageView1;
ViewSwitcher viewSwitcher;
ImageAdapter adapter = new ImageAdapter(getParent(), Album3Activity.bmpArray, Album3Activity.fileName);
Button avbryt, camera, trash, opendialog, slett, 
    avbryt1, epost, mms, avbryt2, facebook;
TextView textView;
ImageButton btnNext, btnPrevious;
GestureDetector gesturedetector = null;
Intent in; 
int touches; 
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen);
    viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
    imageView1 = (ImageView)findViewById(R.id.imageView1);
    textView = (TextView)findViewById(R.id.textView1);

    in = getIntent();
    touches = in.getExtras().getInt("id");
    imageView1.setImageBitmap((Bitmap)adapter.getItem(touches)); 
    textView.setText(ImageAdapter.readName(touches));

    btnNext = (ImageButton)findViewById(R.id.btnNext);
    btnNext.setOnClickListener(this);

    btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
    btnPrevious.setOnClickListener(this);

    trash = (Button)findViewById(R.id.trash);
    trash.setOnClickListener(this);

    opendialog = (Button)findViewById(R.id.opendialog);
    opendialog.setOnClickListener(this);

    avbryt = (Button)findViewById(R.id.avbryt);
    avbryt.setOnClickListener(this);

    camera = (Button)findViewById(R.id.camera_albumbutton);
    camera.setOnClickListener(this);

    gesturedetector=new GestureDetector(this, this);
}//onCreate

    public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnNext:
        Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);
        nextView();
        break;

    case R.id.btnPrevious:
        Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);
        previousView();
        break;

    case R.id.trash:
        final Dialog dialog  = new Dialog(FullScreen.this);
        dialog.setContentView(R.layout.trashdialog);
        slett = (Button)dialog.findViewById(R.id.slett);
        avbryt1 = (Button)dialog.findViewById(R.id.avbryt);

        slett.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory()+
                            "/diplomat/"+ ImageAdapter.readName(touches));
                Log.d("FullScreen Path", file.getAbsolutePath());
                Log.d("Hi1 touches value", ""+touches);
                try
                {

                    if(touches == (Album3Activity.bmpArray.size() - 1))
                    {
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
                        Album3Activity.bmpArray.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());

                        Album3Activity.fileName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        touches = Album3Activity.bmpArray.size() - touches;
                        Log.d("FullScreen delete:if block", "Updated touches value:"+touches);

                        do
                        {
                            Log.d("FullScreen:delete if", "in while loop");
                            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                            textView.setText(ImageAdapter.readName(touches));
                            break;
                        }//do
                        while(Album3Activity.bmpArray.isEmpty() == false);
                    }//if
                    else
                    {
                        Log.d("fullscreen:delete else", "else block executed");
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

                        Album3Activity.bmpArray.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());

                        Album3Activity.fileName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                        textView.setText(ImageAdapter.readName(touches));

                    }//else
                }//try
                catch(IndexOutOfBoundsException e)
                {
                    Log.d("Fullscreen delete method", e.toString());
                    Toast.makeText(FullScreen.this, "No more image left to diaplay"
                            , Toast.LENGTH_SHORT).show();
                }//catch
                finally
                {
                    dialog.dismiss();
                }//finally
            }//onClick
        });

        avbryt1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }//onClick
        });
        dialog.show();
        break;

    case R.id.opendialog:
        final Dialog dialog1 = new Dialog(FullScreen.this);
        dialog1.setContentView(R.layout.openbuttondialog);
        epost = (Button)dialog1.findViewById(R.id.epost);
        mms = (Button)dialog1.findViewById(R.id.mms);
        facebook = (Button)dialog1.findViewById(R.id.facebook);
        avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);

        epost.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        mms.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        facebook.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        avbryt2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog1.cancel();
            }//onClick
        });
        dialog1.show();
        break;

    case R.id.avbryt:
        break;

    case R.id.camera_albumbutton:
        break;

    default:
        break;
    }//switch

}//onClick

private void previousView() {
    if(touches > 0){
        touches = touches - 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(ImageAdapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation1);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
        viewSwitcher.showPrevious();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "previousView");
}//previousView

private void nextView() {
    if(touches < Album3Activity.bmpArray.size() - 1){
        touches = touches + 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(ImageAdapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation);
        viewSwitcher.showNext();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "nextView");
}//nextView

public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return true;
}


int SWIPE_MIN_VELOCITY = 50;
int SWIPE_MIN_DISTANCE = 50;


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float ev1X = e1.getX();
    float ev2X = e2.getX();
    Log.d("On", "Onfling");
    //Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    //Get veclocity of cusor
    //Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    //Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối > 100
    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)//Switch Left
        {
            previousView();
        }
        else//Switch Right
        {
            nextView();
        }
    }

    return false;
}

public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("on touch","touch");
    // TODO Auto-generated method stub
    //return gesturedetector.onTouchEvent(event);

    this.gesturedetector.onTouchEvent(event);
      return super.onTouchEvent(event);
}

    }//FullScreen

Upvotes: 0

Views: 1475

Answers (3)

Nitish
Nitish

Reputation: 3155

AlbumActivity

public class Album3Activity extends Activity {
File [] mediaFiles;
File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
String name = null;
ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    //Log.d("Length of images",""+mediaFiles.length);
    try
    {
        for(File file : mediaFiles)
        {
            bmpArray.add(convertToBitmap(file));
            fileName.add(readFileName(file));
            Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
            Log.d(TAG, "call to convertToBitmap");
        }//for
    }//try
    catch(Exception e)
    {
        Log.d(TAG, e.toString());
        Toast.makeText(Album3Activity.this, "No files available", Toast.LENGTH_SHORT).show();
    }//catch
    adapter = new ImageAdapter(this, bmpArray, fileName);
    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 convertToBitmap(File file) 
{
    URL url = null;
    try 
    {
        url = file.toURL();
    } catch (MalformedURLException e1) 
    {
        Log.d(TAG, e1.toString());
    }//catch

    Bitmap bmp = null;
    try
    {
       bmp = BitmapFactory.decodeStream(url.openStream());
       //bmp.recycle();
    }catch(Exception e)
    {
        Log.d(TAG, "Exception: "+e.toString());
    }//catch
    return bmp;
}//convertToBitmap

public String readFileName(File file){
    String name = file.getName();
    return name;
}//readFileName
}//class

ImageAdapter

public class ImageAdapter extends BaseAdapter {
Activity act;
static ArrayList<Bitmap> mFiles;
static ArrayList<String> mName;
public static final String TAG = "ImageAdapter";
String name = null;
public ImageAdapter(Activity act, ArrayList<Bitmap> mFiles, ArrayList<String> mName) {
    super();
    this.act = act;
    ImageAdapter.mFiles = mFiles;
    ImageAdapter.mName = mName;
    Log.d("mfiles", "" + ImageAdapter.mFiles.size());
}// ImageAdapter

public int getCount() {
    return mFiles.size();
}// getCount

public Object getItem(int postion) {
    return mFiles.get(postion);
}// getItem

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

public static class ViewHolder {
    ImageView iv;
    TextView tv;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    LayoutInflater li = act.getLayoutInflater();
    if (convertView == null) {
        Log.d("tag", "null");
        view = new ViewHolder();
        convertView = li.inflate(R.layout.gridview_row, null);
        view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
        view.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(view);
    }// if
    else {
        view = (ViewHolder) convertView.getTag();
    }// else
    view.iv.setImageBitmap(mFiles.get(position));
    view.tv.setText(readName(position));
    Log.d(TAG, "getView method called");
    return convertView;
}// getView

public String readName(int position) {
    String name = mName.get(position);
    return name;
}//readName

}// ImageAdapter

FullScreen

public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
ImageView imageView1;
ViewSwitcher viewSwitcher;
ImageAdapter adapter = new ImageAdapter(getParent(), ImageAdapter.mFiles, ImageAdapter.mName);
Button avbryt, camera, trash, opendialog, slett, 
    avbryt1, epost, mms, avbryt2, facebook;
TextView textView;
ImageButton btnNext, btnPrevious;
GestureDetector gesturedetector = null;
Intent in; 
int touches; 

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen);
    viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
    imageView1 = (ImageView)findViewById(R.id.imageView1);
    textView = (TextView)findViewById(R.id.textView1);

    in = getIntent();
    touches = in.getExtras().getInt("id");
    imageView1.setImageBitmap((Bitmap)adapter.getItem(touches)); 
    textView.setText(adapter.readName(touches));
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
            , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

    btnNext = (ImageButton)findViewById(R.id.btnNext);
    btnNext.setOnClickListener(this);

    btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
    btnPrevious.setOnClickListener(this);

    trash = (Button)findViewById(R.id.trash);
    trash.setOnClickListener(this);

    opendialog = (Button)findViewById(R.id.opendialog);
    opendialog.setOnClickListener(this);

    avbryt = (Button)findViewById(R.id.avbryt);
    avbryt.setOnClickListener(this);

    camera = (Button)findViewById(R.id.camera_albumbutton);
    camera.setOnClickListener(this);

    gesturedetector=new GestureDetector(this, this);
}//onCreate

public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnNext:
        Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);
        nextView();
        break;

    case R.id.btnPrevious:
        Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);
        previousView();
        break;

    case R.id.trash:
        final Dialog dialog  = new Dialog(FullScreen.this);
        dialog.setContentView(R.layout.trashdialog);
        slett = (Button)dialog.findViewById(R.id.slett);
        avbryt1 = (Button)dialog.findViewById(R.id.avbryt);

        slett.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory()+
                            "/diplomat/"+ adapter.readName(touches));
                Log.d("FullScreen Path", file.getAbsolutePath());
                Log.d("Hi1 touches value", ""+touches);
                try
                {

                    if(touches == (ImageAdapter.mFiles.size() - 1))
                    {
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
                        ImageAdapter.mFiles.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+ImageAdapter.mFiles.size());

                        ImageAdapter.mName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+ImageAdapter.mName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        touches = ImageAdapter.mFiles.size() - touches;
                        Log.d("FullScreen delete:if block", "Updated touches value:"+touches);

                        do
                        {
                            Log.d("FullScreen:delete if", "in while loop");
                            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                            textView.setText(adapter.readName(touches));
                            break;
                        }//do
                        while(ImageAdapter.mFiles.isEmpty() == false);
                    }//if
                    else
                    {
                        Log.d("fullscreen:delete else", "else block executed");
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

                        ImageAdapter.mFiles.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+ImageAdapter.mFiles.size());

                        ImageAdapter.mName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+ImageAdapter.mName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                        textView.setText(adapter.readName(touches));

                    }//else
                }//try
                catch(IndexOutOfBoundsException e)
                {
                    Log.d("Fullscreen delete method", e.toString());
                    Toast.makeText(FullScreen.this, "No more image left to diaplay"
                            , Toast.LENGTH_SHORT).show();
                }//catch
                finally
                {
                    dialog.dismiss();
                }//finally
            }//onClick
        });

        avbryt1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }//onClick
        });
        dialog.show();
        break;

    case R.id.opendialog:
        final Dialog dialog1 = new Dialog(FullScreen.this);
        dialog1.setContentView(R.layout.openbuttondialog);
        epost = (Button)dialog1.findViewById(R.id.epost);
        mms = (Button)dialog1.findViewById(R.id.mms);
        facebook = (Button)dialog1.findViewById(R.id.facebook);
        avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);

        epost.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        mms.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        facebook.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        avbryt2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog1.cancel();
            }//onClick
        });
        dialog1.show();
        break;

    case R.id.avbryt:
        break;

    case R.id.camera_albumbutton:
        break;

    default:
        break;
    }//switch

}//onClick

private void previousView() {
    if(touches > 0){
        touches = touches - 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(adapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation1);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
        viewSwitcher.showPrevious();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "previousView");
}//previousView

private void nextView() {
    if(touches < ImageAdapter.mFiles.size() - 1){
        touches = touches + 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(adapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation);
        viewSwitcher.showNext();
    //}//if
    //else{
        //Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
        while(touches == (ImageAdapter.mFiles.size() - 1))
        {
            touches = 0;
            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
            touches = touches + 1;
            break;
        }//while
    }//else
    Log.d("Switcher", "nextView");
}//nextView

public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return true;
}


int SWIPE_MIN_VELOCITY = 50;
int SWIPE_MIN_DISTANCE = 50;


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float ev1X = e1.getX();
    float ev2X = e2.getX();
    Log.d("On", "Onfling");
    //Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    //Get veclocity of cusor
    //Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    //Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối > 100
    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)//Switch Left
        {
            previousView();
        }
        else//Switch Right
        {
            nextView();
        }
    }

    return false;
}

public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("on touch","touch");
    // TODO Auto-generated method stub
    //return gesturedetector.onTouchEvent(event);

    this.gesturedetector.onTouchEvent(event);
      return super.onTouchEvent(event);
}

}//FullScreen

Upvotes: 0

Nishant
Nishant

Reputation: 32222

You declared all your variables as static? static variable maintains only one copy in the application. It adds the fileName to the same copy. If you don't want duplicate values better use HashSet instead of ArrayList.

static HashSet<Bitmap> bmpArray = new HashSet<Bitmap>();
static HashSet<String> fileName = new HashSet<String>();

Upvotes: 2

Barak
Barak

Reputation: 16393

Right now when you leave the app a bundle is being created, and when you return it is used by the system to restore you to where you were (giving you your original seven) and then you are adding them again because onCreate runs again.

You need to check for a savedInstanceState, and if it exists, don't do anything. You should have this in all of your onCreate methods.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    if (savedInstanceState == null) { 
         // Do your oncreate stuff because there is no bundle    
    } 
// Do stuff that needs to be done even if there is a saved instance, or do nothing 
} 

Upvotes: 0

Related Questions