0

Hello stackoverflow I'm trying to develop an application that display images from SD card and allow user to delete the images using a check box. I'm able to display images from SD card with CheckBox but I'm unable to delete the specific image which has been ticked by the user dynamically. Here is my MainActivity.java

public class MainActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
Button btnDelete;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getFromSdcard();
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);

    imageAdapter.notifyDataSetChanged();

    btnDelete = (Button) findViewById(R.id.btnDeleteImg);

    btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // to delete selected images

        }
    });
}

public void getFromSdcard() {
    File file = new File("/mnt/sdcard/Images");

    if (file.isDirectory()) {
        listFile = file.listFiles();

        for (int i = 0; i < listFile.length; i++) {

            f.add(listFile[i].getAbsolutePath());

        }
    }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}

class ViewHolder {
    ImageView imageview;
}

}

My activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.97"
    android:columnWidth="90sp"
    android:gravity="center"
    android:horizontalSpacing="10sp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10sp" />

<RelativeLayout
    android:id="@+id/rlBookmark"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:layout_gravity="bottom" >

    <Button
        android:id="@+id/btnBookmark"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Clear"
        android:textColor="#FF0011"
        android:textSize="15sp"
        android:textStyle="normal" />
</RelativeLayout>

</LinearLayout>

and finally my galleryitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="100sp"
    android:layout_height="100sp" />

<CheckBox
    android:id="@+id/itemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

Please help me to solve this riddle, thanks in advance stackoverflow.

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45

2 Answers2

0

You can refer to this example Images with checkbox and on click of delete button get the image name and perform delete operation like this:

File file= new File(Environment.getExternalStorageDirectory()+ "imageName");
if(file.exists())
{
     file.delete();
}
Srikanth Roopa
  • 1,782
  • 2
  • 13
  • 19
  • How can I delte only checked images ? – Chethan Shetty Jul 23 '13 at 05:43
  • u can Chetan in that example he's setting the boolean array values to true or false based on check box selection and based on click loop through the arraylist get the image name and pass that to delete fucntion – Srikanth Roopa Jul 23 '13 at 05:54
  • 1
    get the id of selected image, then get the file path of that id. then use the for loop, trigger the delete command for every selected id. – RAAAAM Jul 23 '13 at 07:47
0

Hi stackoverflow finally i figured out how to delete multiple images using CheckBox in a GridView

public class MainActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;

Button btnDelete;

private ProgressDialog pd;

HashSet<String> selectedFile = new HashSet<String>();// list of file paths boolean checked

GridView imagegrid;

AlertDialog alertDialog = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFromSdcard();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);

imageAdapter.notifyDataSetChanged();

btnDelete = (Button) findViewById(R.id.btnDeleteImg);

btnDelete.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // to delete selected images

    }
});
}

public void getFromSdcard() {
File file = new File("/mnt/sdcard/Images");

if (file.isDirectory()) {
    listFile = file.listFiles();

    for (int i = 0; i < listFile.length; i++) {

        f.add(listFile[i].getAbsolutePath());

    }
}
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);

        final int pos = position;

        holder.checkbox.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(!selectedFile.contains((String)f.get(pos)))
                {
                    selectedFile.add((String)f.get(pos));
                }
                else
                {
                    selectedFile.remove((String)f.get(pos));
                }
            }
        });
        return convertView;
    }
}

class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}
}

This approach is working for me, if you guys need more information please comment, Thanks stackoverflow

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45
  • this is working fine but in my case when we delete the multiple image then image will be deleted but checkbox dosent hidden and also imageview row. Only image path will be deleted from that method. – Google Sep 18 '15 at 10:15
  • Please can you tell me What is selectedFile and f in this syntax if(!selectedFile.contains((String)f.get(pos))). – androidTag Oct 13 '15 at 04:42
  • @Chetan Shetty : I also trying to implement for delete selected multiplr image from grid which is displaying from camera and gallery .Can you help me to solve the issue . – androidTag Oct 28 '15 at 05:21