i want to get path of selected image from gridview in android ? i'm using recycler for this gridview -
i'm developing app in selected images can upload server @ time...for need path of selected images in array , have pass array upload activity...so images can uploaded server 1 one automatically....
here adapter class
package com.akshay.nmiua;
import android.content.context; import android.support.v7.widget.recyclerview; import android.util.sparsebooleanarray; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.imageview; import com.bumptech.glide.glide ; import com.technotalkative.multiphotoselect.r; import java.util.arraylist; public class imageadapter extends recyclerview.adapter<imageadapter.myviewholder> { private arraylist<string> mimageslist; private context mcontext; private sparsebooleanarray msparsebooleanarray; public imageadapter(context context, arraylist<string> imagelist) { mcontext = context; msparsebooleanarray = new sparsebooleanarray(); mimageslist = new arraylist<string>(); this.mimageslist = imagelist; } public arraylist<string> getcheckeditems() { arraylist<string> mtemparry = new arraylist<string>(); for(int i=0;i<mimageslist.size();i++) { if(msparsebooleanarray.get(i)) { mtemparry.add(mimageslist.get(i)); } } return mtemparry; } @override public long getitemid(int position) { return position; } compoundbutton.oncheckedchangelistener mcheckedchangelistener = new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { msparsebooleanarray.put((integer) buttonview.gettag(), ischecked); } }; @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()) .inflate(r.layout.row_multiphoto_item, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(myviewholder holder, int position) { string imageurl = mimageslist.get(position); glide.with(mcontext) .load("file://"+imageurl) .centercrop() .placeholder(r.drawable.ic_launcher) .error(r.drawable.ic_launcher) .into(holder.imageview); holder.checkbox.settag(position); holder.checkbox.setchecked(msparsebooleanarray.get(position)); holder.checkbox.setoncheckedchangelistener(mcheckedchangelistener); } @override public int getitemcount() { return mimageslist.size(); } public class myviewholder extends recyclerview.viewholder { public checkbox checkbox; public imageview imageview; public myviewholder(view view) { super(view); checkbox = (checkbox) view.findviewbyid(r.id.checkbox1); imageview = (imageview) view.findviewbyid(r.id.imageview1); } } }
here itemoffset class..
package com.akshay.nmiua; import android.content.context; import android.graphics.rect; import android.support.annotation.dimenres; import android.support.annotation.nonnull; import android.support.v7.widget.recyclerview; import android.view.view; public class itemoffsetdecoration extends recyclerview.itemdecoration { private int mitemoffset; public itemoffsetdecoration(int itemoffset) { mitemoffset = itemoffset; } public itemoffsetdecoration(@nonnull context context, @dimenres int itemoffsetid) { this(context.getresources().getdimensionpixelsize(itemoffsetid)); } @override public void getitemoffsets(rect outrect, view view, recyclerview parent, recyclerview.state state) { super.getitemoffsets(outrect, view, parent, state); outrect.set(mitemoffset, mitemoffset, mitemoffset, mitemoffset); } }
here main activity..
package com.akshay.nmiua; import android.content.pm.packagemanager; import android.database.cursor; import android.os.build; import android.os.bundle; import android.provider.mediastore; import android.support.annotation.nonnull; import android.support.design.widget.snackbar; import android.support.v4.app.activitycompat; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.defaultitemanimator; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.view; import android.widget.toast; import com.technotalkative.multiphotoselect.r; import java.util.arraylist; import static android.manifest.permission.read_external_storage; public class multiphotoselectactivity extends appcompatactivity { private imageadapter imageadapter; private static final int request_for_storage_permission = 123; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_multi_photo_select); populateimagesfromgallery(); } public void btnchoosephotosclick(view v){ arraylist<string> selecteditems = imageadapter.getcheckeditems(); if (selecteditems!= null && selecteditems.size() > 0) { toast.maketext(multiphotoselectactivity.this, "total photos selected: " + selecteditems.size(), toast.length_short).show(); log.d(multiphotoselectactivity.class.getsimplename(), "selected items: " + selecteditems.tostring()); } } private void populateimagesfromgallery() { if (!mayrequestgalleryimages()) { return; } arraylist<string> imageurls = loadphotosfromnativegallery(); initializerecyclerview(imageurls); } private boolean mayrequestgalleryimages() { if (build.version.sdk_int < build.version_codes.m) { return true; } if (checkselfpermission(read_external_storage) == packagemanager.permission_granted) { return true; } if (shouldshowrequestpermissionrationale(read_external_storage)) { showpermissionrationalesnackbar(); } else { requestpermissions(new string[]{read_external_storage}, request_for_storage_permission); } return false; } @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { switch (requestcode) { case request_for_storage_permission: { if (grantresults.length > 0) { if (grantresults[0] == packagemanager.permission_granted) { populateimagesfromgallery(); } else { if (activitycompat.shouldshowrequestpermissionrationale(this, read_external_storage)) { showpermissionrationalesnackbar(); } else { toast.maketext(this, "go settings , enable permission", toast.length_long).show(); } } } break; } } } private arraylist<string> loadphotosfromnativegallery() { final string[] columns = { mediastore.images.media.data, mediastore.images.media._id }; final string orderby = mediastore.images.media.date_taken; cursor imagecursor = managedquery( mediastore.images.media.external_content_uri, columns, null, null, orderby + " desc"); arraylist<string> imageurls = new arraylist<string>(); (int = 0; < imagecursor.getcount(); i++) { imagecursor.movetoposition(i); int datacolumnindex = imagecursor.getcolumnindex(mediastore.images.media.data); imageurls.add(imagecursor.getstring(datacolumnindex)); system.out.println("=====> array path => "+imageurls.get(i)); } return imageurls; } private void initializerecyclerview(arraylist<string> imageurls) { imageadapter = new imageadapter(this, imageurls); recyclerview.layoutmanager layoutmanager = new gridlayoutmanager(getapplicationcontext(),4); recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); recyclerview.setlayoutmanager(layoutmanager); recyclerview.setitemanimator(new defaultitemanimator()); recyclerview.additemdecoration(new itemoffsetdecoration(this, r.dimen.item_offset)); recyclerview.setadapter(imageadapter); } private void showpermissionrationalesnackbar() { snackbar.make(findviewbyid(r.id.button1), getstring(r.string.permission_rationale), snackbar.length_indefinite).setaction("ok", new view.onclicklistener() { @override public void onclick(view view) { activitycompat.requestpermissions(multiphotoselectactivity.this, new string[]{read_external_storage}, request_for_storage_permission); } }).show(); } }
Comments
Post a Comment