android - Delete image files from internal storage and arraylist -
basically, i'm saving images in internal storage's directory. i'm showing them in recyclerview using arraylist. there cross button on each image in row layout(see screenshot). need delete image per position not recyclerview internal directory. able remove image recyclerview's arraylist image in directory not gets deleted.
// method in activity show images int. storage recyclerview. private void showimages(){ file directory = new file(file_path); file newfile[] = directory.listfiles(); log.e("sammy_arrlistsize_befor"," "+finalimages.size()); if(finalimages!=null )finalimages.clear(); (int i= 0; < newfile.length; i++) { string path = "file://"+newfile[i].getabsolutepath(); finalimages.add(path); log.e("sammy_imagepath"," "+path); } log.e("sammy_arrlistsize_after"," "+finalimages.size()); recyladapter.notifydatasetchanged(); }
//adapter public class recycleimageadapter extends recyclerview.adapter<recycleimageadapter.viewholder> { private list<string> itemlist; private context mcontext; public recycleimageadapter(context context, list<string> itemlist) { this.itemlist = itemlist; this.mcontext = context; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.imagelayout, parent, false); return new viewholder(view); } @override public void onbindviewholder(final viewholder holder, int position) { uri uri = uri.parse(itemlist.get(position)); holder.setimageview(uri); } @override public int getitemcount() { return this.itemlist.size(); } class viewholder extends recyclerview.viewholder implements view.onclicklistener{ imageview image, delbtn; viewholder(view itemview) { super(itemview); image = (imageview) itemview.findviewbyid(r.id.img_thumbnail); delbtn = (imageview)itemview.findviewbyid(r.id.del); delbtn.setonclicklistener(this); } public void setimageview(uri uri){ picasso.with(mcontext).load(uri).into(new target() { @override public void onbitmaploaded(bitmap bitmap, picasso.loadedfrom from) { image.setimagebitmap(bitmap); } @override public void onbitmapfailed(drawable errordrawable) { log.e("sammy_error"," "+errordrawable); } @override public void onprepareload(drawable placeholderdrawable) { } }); } @override public void onclick(view view) { //here i'm deleting image int position = getadapterposition(); toast.maketext(mcontext, itemlist.get(position), toast.length_short).show(); file file = new file(itemlist.get(position)); file.delete(); itemlist.remove(position); notifyitemremoved(position); notifyitemrangechanged(position, itemlist.size()); } } }
in on click mehod:
take file path
string path = "file://"+newfile[i].getabsolutepath(); file file = new file(path); file.delete();
try code.
Comments
Post a Comment