android - show images from internal storage in Recyclerview -
i'm saving images camera , gallery(multi-select) folder in internal storage. want display images in horizontal recyclerview. in row layout, there close button along imageview. want delete image(file) internal storage when close button clicked. i've 2 problems: problem 1: when select images(single or multiple) gallery, images saving correctly in recyclerview, image gallery shown in position i.e. if there 2 images in folder , select 1 image gallery, in recyclerview 3 positions have image gallery only. problem 2: how delete images folder when close button clicked?
//recyclerview 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 recycleimageadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.imagelayout, parent, false); return new recycleimageadapter.viewholder(view); } @override public void onbindviewholder(final recycleimageadapter.viewholder holder, int position) { (int i= 0; < itemlist.size(); i++) { uri uri = uri.parse(itemlist.get(i)); 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) { int position = getadapterposition(); toast.maketext(mcontext, itemlist.get(position), toast.length_short).show(); itemlist.remove(position); notifyitemremoved(position); notifyitemrangechanged(position, itemlist.size()); //here i'm getting arrayindexoutofboundsexception file file = new file(itemlist.get(position)); file.delete(); } } }
//in fragment, code load images file directory = new file(file_path); file newfile[] = directory.listfiles(); //finalimages arraylist<string> if(finalimages!=null )finalimages.clear(); (int i= 0; < newfile.length; i++) { string path = "file://"+newfile[i].getabsolutepath(); finalimages.add(path); } recyladapter.notifydatasetchanged();
well don't understand first part explain more.
and in 2nd part simple set image path imageview tag.
imageview.settag(imageabsolutepath);
and when click on image tag image view , convert file this
file path = new file(imageview.gettag().tostring()); path.delete();
Comments
Post a Comment