web - Upload image from devide to FTP using android app -
hey have code let user pick image gallery , after chooses image shown in image view, when user click button should upload image ftp server , reason app tells me location of file giving not found.
here ftp upload code (i execute using asynctask)
public uploadimage(string host,int port,string username,string password,string imagepath) { this.host = host; this.port = port; this.username = username; this.password = password; this.imagepath = imagepath; } public void uploadingfilestoftp() throws ioexception { ftpclient con = null; try { con = new ftpclient(); con.connect(host); if (con.login(username, password)) { con.enterlocalpassivemode(); // important! con.setfiletype(ftp.binary_file_type); uri uri = uri.parse(this.imagepath); string data = uri.getpath(); fileinputstream in = new fileinputstream(new file(data)); boolean result = con.storefile("/img.png", in); in.close(); if (result) log.v("upload result", "succeeded"); con.logout(); con.disconnect(); } } catch (exception e) { e.printstacktrace(); } }
and loading file imageview
loadedimage = (imageview)findviewbyid(r.id.upload_image1); drawable drawable = loadedimage.getdrawable(); if(drawable.getconstantstate().equals(getresources().getdrawable(r.drawable.camera_icon).getconstantstate())) { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "selectpicture"), select_picture); } public void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == result_ok) { if (requestcode == select_picture) { selectedimageuri = data.getdata(); loadedimage = (imageview)findviewbyid(r.id.upload_image1); loadedimage.setscaletype(imageview.scaletype.center_crop); glide.with(this).load(selectedimageuri) .into((imageview) findviewbyid(r.id.upload_image1)); imageview m =(imageview)findviewbyid(r.id.remove_image); m.setfocusable(true); m.setvisibility(view.visible); } } }
the imagepath string image uri converted string. can me , tell me why isn't finding file?
ask user pick image , use image imageview.. path
consist of actual image path can upload it..
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == pick_image_request && resultcode == result_ok && data != null && data.getdata() != null) { uri = data.getdata(); if (uri != null) { path = uri.tostring(); if (path.tolowercase().startswith("file://")) { // selected file/directory path below path = (new file(uri.create(path))).getabsolutepath(); } else{ path = getrealpathfromuri(getapplicationcontext(),uri); } } try { bitmap = mediastore.images.media.getbitmap(getcontentresolver(), uri); profileimg.setimagebitmap(bitmap); } catch (ioexception e) { e.printstacktrace(); } } } public static string getrealpathfromuri(context context, uri contenturi) { cursor cursor = null; try { string[] proj = { mediastore.images.media.data }; cursor = context.getcontentresolver().query(contenturi, proj, null, null, null); int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); return cursor.getstring(column_index); } { if (cursor != null) { cursor.close(); } } }
hope giving permission also
Comments
Post a Comment