listview - Android: Get multiple images from Gallery and Populate them in a list view in android -
i want multiple images gallery , populate images in listview. first of all, possible without using third party libraries?
this doing currently.... using universal_image_loader multiple images gallery, not able populate them in listview.
"main.xml" - imagelist list containing images gallery
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.85" android:orientation="horizontal" > <imageview android:id="@+id/image1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_gravity="left" android:layout_weight="1" /> <listview android:id="@+id/imagelist" android:layout_width="70dp" android:layout_height="match_parent" android:layout_gravity="right" android:layout_weight="1" > </listview> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/button_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="getphoto" android:text="gallery" /> </linearlayout> </linearlayout>
"list_row.xml" - view of single row of list
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <imageview android:id="@+id/imagethumb" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/index"/> <textview android:id="@+id/listtext" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
"mainactivity.java"
public class mainactivity extends activity { button buttonphoto; imageview image1; listview imagelist; listadapter adapter; arraylist<string> selectedphotoslist = null; string[] selectedphotos = null; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.main); buttonphoto = (button) findviewbyid(r.id.button_photo); imagelist = (listview) findviewbyid(r.id.imagelist); adapter = new listadapter(this, selectedphotoslist); imagelist.setadapter(adapter); } public void getphoto(view view) { intent = new intent(this, multiphotoselectactivity.class); startactivityforresult(i, 0); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode == 0) { selectedphotoslist = data.getstringarraylistextra("selectedphotos"); //refresh list adapter.notifydatasetchanged(); log.d(multiphotoselectactivity.class.getsimplename(), "selected photos: " + selectedphotos); } } }
"listadapter.java" - adapter list populating images gallery
public class listadapter extends baseadapter { private static layoutinflater inflater=null; private activity activity; private arraylist<string> imagedata; public listadapter(activity a, arraylist<string> list) { activity = a; imagedata = list; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return imagedata.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { view vi = convertview; if(convertview==null) vi = inflater.inflate(r.layout.list_row, null); imageview thumb_image=(imageview)vi.findviewbyid(r.id.imagethumb); // thumb image textview text=(textview)vi.findviewbyid(r.id.listtext); bitmap mybitmap = bitmapfactory.decodefile(imagedata.get(position)); thumb_image.setimagebitmap(mybitmap); text.settext(position); return vi; } }
when comes mainactivity, don't see images in listview. please me out. doing wrong?
edit
"multiphotoselectactivity.java - returns path of images selected
package com.example.demo1; import java.util.arraylist; import android.content.context; import android.database.cursor; import android.graphics.bitmap; import android.os.bundle; import android.provider.mediastore; import android.util.log; import android.util.sparsebooleanarray; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.baseadapter; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.toast; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.gridview; import android.widget.imageview; import com.nostra13.universalimageloader.core.displayimageoptions; import com.nostra13.universalimageloader.core.assist.simpleimageloadinglistener; public class multiphotoselectactivity extends baseactivity { private arraylist<string> imageurls; private displayimageoptions options; private imageadapter imageadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.ac_image_grid); 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"); this.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)); } options = new displayimageoptions.builder() .showstubimage(r.drawable.stub_image) .showimageforemptyuri(r.drawable.image_for_empty_url) .cacheinmemory() .cacheondisc() .build(); imageadapter = new imageadapter(this, imageurls); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(imageadapter); /*gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { startimagegalleryactivity(position); } });*/ } @override protected void onstop() { imageloader.stop(); super.onstop(); } public void btnchoosephotosclick(view v){ arraylist<string> selecteditems = imageadapter.getcheckeditems(); toast.maketext(multiphotoselectactivity.this, "total photos selected: "+selecteditems.size(), toast.length_short).show(); log.d(multiphotoselectactivity.class.getsimplename(), "selected items: " + selecteditems.tostring()); } /*private void startimagegalleryactivity(int position) { intent intent = new intent(this, imagepageractivity.class); intent.putextra(extra.images, imageurls); intent.putextra(extra.image_position, position); startactivity(intent); }*/ public class imageadapter extends baseadapter { arraylist<string> mlist; layoutinflater minflater; context mcontext; sparsebooleanarray msparsebooleanarray; public imageadapter(context context, arraylist<string> imagelist) { // todo auto-generated constructor stub mcontext = context; minflater = layoutinflater.from(mcontext); msparsebooleanarray = new sparsebooleanarray(); mlist = new arraylist<string>(); this.mlist = imagelist; } public arraylist<string> getcheckeditems() { arraylist<string> mtemparry = new arraylist<string>(); for(int i=0;i<mlist.size();i++) { if(msparsebooleanarray.get(i)) { mtemparry.add(mlist.get(i)); } } return mtemparry; } @override public int getcount() { return imageurls.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if(convertview == null) { convertview = minflater.inflate(r.layout.row_multiphoto_item, null); } checkbox mcheckbox = (checkbox) convertview.findviewbyid(r.id.checkbox1); final imageview imageview = (imageview) convertview.findviewbyid(r.id.imageview1); imageloader.displayimage("file://"+imageurls.get(position), imageview, options, new simpleimageloadinglistener() { @override public void onloadingcomplete(bitmap loadedimage) { animation anim = animationutils.loadanimation(multiphotoselectactivity.this, r.anim.fade_in); imageview.setanimation(anim); anim.start(); } }); mcheckbox.settag(position); mcheckbox.setchecked(msparsebooleanarray.get(position)); mcheckbox.setoncheckedchangelistener(mcheckedchangelistener); return convertview; } oncheckedchangelistener mcheckedchangelistener = new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub msparsebooleanarray.put((integer) buttonview.gettag(), ischecked); } }; } }
you haven't attached multiphotoselectactivity class code, isn't clear result after clicking gallery button. anyway, should use cursorloader query mediastore images instead. , why decoding bitmaps , not use uil (like mentioned) or picasso?
Comments
Post a Comment