i have problem while doing photo gallery. retrieving network. problem images not been able set in gallery. know might doing silly mistake somewhere. can please me this.
here updated code:
mainactivity.java
public class mainactivity extends activity implements onitemclicklistener { string filename = "http://122.com/12.php"; private static final string tag = "main"; private static string carid = ""; private gallery _gallery; private imageadapter _imageadapter; static string j_img1 = null; static string j_img2 = null; static string j_img3 = null; static string j_img4 = null; imageview img1; bitmap decodedbyte; sharedpreferences sp; string s_id, is, str; arraylist<hashmap<string, string>> imagelist = new arraylist<hashmap<string, string>>(); static arraylist<bitmap> bitimg = new arraylist<bitmap>(); // static arraylist<bitmap> img = new arraylist<bitmap>(); private static final string image1 = "img"; private static final string image2 = "img2"; private static final string image3 = "img3"; private static final string image4 = "img4"; /** called when activity first created. */ public void getcarid(string car_id) { // todo auto-generated method stub carid = car_id; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); settitle("android honeycomb photo gallery example"); sp = preferencemanager.getdefaultsharedpreferences(this); s_id = sp.getstring("logid", null); toast.maketext(getapplicationcontext(), "id= " + s_id, toast.length_long).show(); toast.maketext(getapplicationcontext(), "car_id= " + carid, toast.length_long).show(); //img1 = (imageview) this.findviewbyid(r.id.imageview1); final vibrator vibrator = (vibrator)getsystemservice(vibrator_service); /* img1.setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view v) { vibrator.vibrate(100); alertdialog.builder dialogo1 = new alertdialog.builder( mainactivity.this); dialogo1.setitems(r.array.save, new dialoginterface.onclicklistener() { @suppresslint("showtoast") public void onclick( dialoginterface dialoginterface, int i) { if (i == 0) { saveimage(); } } }).show(); return true; } });*/ _gallery = (gallery) this.findviewbyid(r.id.gallery1); startdownload(); _gallery.setadapter(_imageadapter); _gallery.setonitemclicklistener(this); } public void send_img() { toast.maketext(this, "send_img", toast.length_short).show(); _imageadapter = new imageadapter(this); _imageadapter.setimage(bitimg); } void saveimage() { // string root = environment.getexternalstoragedirectory().tostring(); file mydir = new file("/sdcard/carlist/"); long tslong = system.currenttimemillis()/1000; string ts = tslong.tostring(); string fname = ts+".jpg"; toast.maketext(getapplicationcontext(), "string name=:" + fname, toast.length_long).show(); file file = new file (mydir, fname); if (file.exists ()) file.delete (); try { fileoutputstream out = new fileoutputstream(file); decodedbyte.compress(bitmap.compressformat.jpeg, 90, out); out.flush(); out.close(); } catch (exception e) { e.printstacktrace(); } } @override public void onitemclick(adapterview arg0, view view, int position, long duration) { int resourcid = (integer) _imageadapter.getitem(position); toast.maketext(getapplicationcontext(), "inside onitemclick= ", toast.length_long).show(); // drawable drawable =getresources().getdrawable(resourcid); /* bitmap bitmap = bitmapfactory.decoderesource(getresources(), resourcid); toast.maketext( this, "selected image: " + getresources().gettext(resourcid) + "\nheight: " + bitmap.getheight() + "\nwidth: " + bitmap.getwidth(), toast.length_short).show();*/ } private void startdownload() { toast.maketext(this, "startdownload", toast.length_short).show(); new apptask().execute(filename); // return bitimg; } public class apptask extends asynctask<string, integer, string> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(string result) { super.onpostexecute(result); str = result; toast.maketext(getapplicationcontext(), "array= " + str, toast.length_long).show(); try { jsonarray jarray = new jsonarray(str); (int = 0; < jarray.length(); i++) { jsonobject jobject = jarray.getjsonobject(i); j_img1 = jobject.getstring(image1); j_img2 = jobject.getstring(image2); j_img3 = jobject.getstring(image3); j_img4 = jobject.getstring(image4); system.out.println("image array = " + jarray); hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(image1, j_img1); map.put(image2, j_img2); map.put(image3, j_img3); map.put(image4, j_img4); imagelist.add(map); (int j = 0; j < imagelist.size(); j++) { iterator myveryowniterator = map.keyset().iterator(); while (myveryowniterator.hasnext()) { string key = (string) myveryowniterator.next(); string value = (string) map.get(key); byte[] decodedstring = base64.decode(value, base64.default); decodedbyte = bitmapfactory.decodebytearray( decodedstring, 0, decodedstring.length); //img1.setimagebitmap(decodedbyte); bitimg.add(decodedbyte); } } //img1.setimagebitmap(decodedbyte); } toast.maketext(getapplicationcontext(), "bitimg size= " + bitimg.size(), toast.length_long) .show(); send_img(); } catch (jsonexception e) { e.printstacktrace(); } } @override protected void onprogressupdate(integer... values) { // todo auto-generated method stub super.onprogressupdate(values); } @override protected string doinbackground(string... params) { // todo auto-generated method stub httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(filename); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>( 1); namevaluepairs.add(new basicnamevaluepair("cid", carid)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entityutils.tostring(entity); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block } return is; } }
}
and updated imageadapter.java
public class imageadapter extends baseadapter { private context _context = null; string s; public static arraylist<bitmap> imageids; string size; public imageadapter(context context) { this._context = context; } @override public int getcount() { // todo auto-generated method stub toast.maketext(_context, "getcount= " + imageids.size(), toast.length_long).show(); return (imageids == null) ? 0 : imageids.size(); } @override public object getitem(int index) { return imageids.get(index); } @override public long getitemid(int index) { return index; } @override public view getview(int postion, view view, viewgroup group) { imageview imageview = new imageview(_context); toast.maketext(_context, "outside forloop= ", toast.length_long).show(); (int = 0; i<=postion;i++) //< imageids.size(); postion++) { toast.maketext(_context, "inside getview= ", toast.length_long) .show(); imageview.setimagebitmap(imageids.get(postion)); imageview.setscaletype(scaletype.fit_xy); imageview.setlayoutparams(new gallery.layoutparams(400, 400)); //imageview.setimageresource(imageids[position]); // system.gc(); } return imageview; } public void setimage(arraylist<bitmap> bitimg) { imageids = bitimg; toast.maketext(_context, "setimage size= " + imageids.size(), toast.length_long).show(); }
}
updated logcat
07-19 08:42:07.623: e/androidruntime(881): fatal exception: main 07-19 08:42:07.623: e/androidruntime(881): java.lang.nullpointerexception 07-19 08:42:07.623: e/androidruntime(881): @ com.carlist.imageadapter.getcount(imageadapter.java:35) 07-19 08:42:07.623: e/androidruntime(881): @ android.widget.absspinner.setadapter(absspinner.java:113) 07-19 08:42:07.623: e/androidruntime(881): @ com.carlist.mainactivity.send_img(mainactivity.java:126) 07-19 08:42:07.623: e/androidruntime(881): @ com.carlist.mainactivity$apptask.onpostexecute(mainactivity.java:244) 07-19 08:42:07.623: e/androidruntime(881): @ com.carlist.mainactivity$apptask.onpostexecute(mainactivity.java:1) 07-19 08:42:07.623: e/androidruntime(881): @ android.os.asynctask.finish(asynctask.java:417) 07-19 08:42:07.623: e/androidruntime(881): @ android.os.asynctask.access$300(asynctask.java:127) 07-19 08:42:07.623: e/androidruntime(881): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:429) 07-19 08:42:07.623: e/androidruntime(881): @ android.os.handler.dispatchmessage(handler.java:99) 07-19 08:42:07.623: e/androidruntime(881): @ android.os.looper.loop(looper.java:123) 07-19 08:42:07.623: e/androidruntime(881): @ android.app.activitythread.main(activitythread.java:3683) 07-19 08:42:07.623: e/androidruntime(881): @ java.lang.reflect.method.invokenative(native method) 07-19 08:42:07.623: e/androidruntime(881): @ java.lang.reflect.method.invoke(method.java:507) 07-19 08:42:07.623: e/androidruntime(881): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 07-19 08:42:07.623: e/androidruntime(881): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 07-19 08:42:07.623: e/androidruntime(881): @ dalvik.system.nativestart.main(native method)
it not going inside getview() method. in need of please if 1 me thankful you.
the time create imageadater
,imageids
null: change getcount
in order check against null values
@override public int getcount() { return (imageids == null) ? 0 : imageids.size(); } @override public view getview(int postion, view view, viewgroup group) { imageview imageview = new imageview(_context); imageview.setimagebitmap(imageids.get(postion)); imageview.setscaletype(scaletype.fit_xy); imageview.setlayoutparams(new gallery.layoutparams(400, 400)); return imageview; } public void send_img() { toast.maketext(this, "send_img", toast.length_short).show(); if (_imageadapter == null) { _imageadapter = new imageadapter(this); _gallery.setadapter(_imageadapter); } _imageadapter.setimage(bitimg); _imageadapter.notifydatasetchanged(); }
Comments
Post a Comment