The output of bitmap always show null in android form -
i have problem java class android upload file remote server.
in form select file on gallery image on smartphone, output of bitmap show null , form start blocked.
log.d("homeactivity.class", "output: " + bitmap);
this beginning make me believe structure whole not correct.
what missing ?
what's wrong code?
i appreciate can give me in working problem.
public class homeactivity extends activity { button btnsend; spinner area; edittext description; imageview viewimage; button b, upload; bitmap bitmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); b = (button) findviewbyid(r.id.btnselectphoto); viewimage = (imageview) findviewbyid(r.id.viewimage); upload = (button) findviewbyid(r.id.button1); area = (spinner) findviewbyid(r.id.my_spinner_new); description = (edittext) findviewbyid(r.id.edittext); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { selectimage(); } }); upload.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (area.getselecteditem().tostring().trim() .equalsignorecase("select area")) { toast.maketext(homeactivity.this, "area.", toast.length_short).show(); } else if (description.gettext().tostring().length() <= 0) { description.seterror("description"); } else if (bitmap == null) { toast.maketext(getapplicationcontext(), "pic", toast.length_short) .show(); log.d("homeactivity.class", "output: " + bitmap); } else { progressdialog.show(homeactivity.this, "uploading" + bitmap, "please wait...", true); } } }); new thread() { @override public void run() { string path = "http://localhost/list.txt"; url u = null; try { u = new url(path); httpurlconnection c = (httpurlconnection) u .openconnection(); c.setrequestmethod("get"); c.connect(); inputstream in = c.getinputstream(); final bytearrayoutputstream bo = new bytearrayoutputstream(); byte[] buffer = new byte[5120 * 512]; in.read(buffer); bo.write(buffer); string s = bo.tostring(); final vector<string> str = new vector<string>(); string[] line = s.split("\n"); int index = 0; while (index < line.length) { str.add(line[index]); index++; } runonuithread(new runnable() { @override public void run() { spinner spinner = (spinner) findviewbyid(r.id.my_spinner_new); arrayadapter<string> adapter = new arrayadapter<string>( homeactivity.this, android.r.layout.simple_spinner_item, str); spinner.setadapter(adapter); try { bo.close(); } catch (ioexception e) { e.printstacktrace(); } } }); } catch (malformedurlexception e) { e.printstacktrace(); } catch (protocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }.start(); } private void selectimage() { final charsequence[] options = { "take photo", "choose gallery", "cancel" }; alertdialog.builder builder = new alertdialog.builder(mainactivity.this); builder.settitle("add photo!"); builder.setitems(options, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { if (options[item].equals("take photo")) { intent intent = new intent(mediastore.action_image_capture); file f = new file(android.os.environment .getexternalstoragedirectory(), "temp.jpg"); intent.putextra(mediastore.extra_output, uri.fromfile(f)); startactivityforresult(intent, 1); } else if (options[item].equals("choose gallery")) { intent intent = new intent( intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(intent, 2); } else if (options[item].equals("cancel")) { dialog.dismiss(); } } }); builder.show(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == 1) { file f = new file(environment.getexternalstoragedirectory() .tostring()); (file temp : f.listfiles()) { if (temp.getname().equals("temp.jpg")) { f = temp; break; } } try { bitmapfactory.options bitmapoptions = new bitmapfactory.options(); bitmap = bitmapfactory.decodefile(f.getabsolutepath(), bitmapoptions); viewimage.setimagebitmap(bitmap); log.d("homeactivity.class", "valore restituito: " + bitmap); string path = android.os.environment .getexternalstoragedirectory() + file.separator + "phoenix" + file.separator + "default"; f.delete(); outputstream outfile = null; file file = new file(path, string.valueof(system .currenttimemillis()) + ".jpg"); try { outfile = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 85, outfile); outfile.flush(); outfile.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } else if (requestcode == 2) { uri selectedimage = data.getdata(); string[] filepath = { mediastore.images.media.data }; cursor c = getcontentresolver().query(selectedimage, filepath, null, null, null); c.movetofirst(); int columnindex = c.getcolumnindex(filepath[0]); string picturepath = c.getstring(columnindex); c.close(); bitmap thumbnail = (bitmapfactory.decodefile(picturepath)); log.w("path of image gallery......******************.........", picturepath + ""); viewimage.setimagebitmap(thumbnail); } } } }
edit#1
upload.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (area.getselecteditem().tostring().trim() .equalsignorecase("select area")) { toast.maketext(homeactivity.this, "area.", toast.length_short).show(); } else if (description.gettext().tostring().length() <= 0) { description.seterror("description"); } else if (bitmap == null) { toast.maketext(getapplicationcontext(), "pic", toast.length_short) .show(); log.d("homeactivity.class", "output: " + bitmap); } else { progressdialog.show(homeactivity.this, "uploading" + bitmap, "please wait...", true); } } });
you starting upload thread in oncreate. , bitmap null. better start thread in onclick handler.
by way: log.d("homeactivity.class", "output: " + bitmap);
. awfull trying print bitmap. if want check if bitmap null so:
if ( bitmap==null ) log.d(tag, "bitmap==null"); else log.d(tag, "we have nice bitmap");
Comments
Post a Comment