Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:51:28+00:00 2026-06-04T01:51:28+00:00

I had developed an album app in which I am using grid View. The

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:51:29+00:00Added an answer on June 4, 2026 at 1:51 am

    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>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had developed a small animation using jquery. But my problem is when i
My last employer had developed an elaborate system which sat on top of SVN
I was wondering if anyone had developed an approach for using NHibernate / Fluent
I had developed an application using .NET 2.0 and database as Sql Server 2005.
Hi there Im new to mobile application development. I had developed Android apps using
i had made my own .deb package for media player developed by me,but when
I had developed an app with target API as 15. The layout includes a
I had developed Android Application which has Login Authentication Page. When the user logs
I had developed an application and the app targets only to a specific country.
I have some source code which I had developed and later gave to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.