android - Unable To Send Data/File From Hotspot Enabled Device. -


i developing app zapya share type of file. have achieved objective problem device hotspot enabled can receive file cannot send file , throws exception. in regard please.

here code sending file

sender activity

public class senderactivity extends appcompatactivity {      spotsdialog dialog;     progressdialog mprogressdialog;     private static final string tag = "mainactivity";     private arraylist<string> docpaths;     private filesender filesender;     button bpickfile, bti;     private static final int file_select_code = 0;     private final handler mhandler = new handler(looper.getmainlooper()) {         @override         public void handlemessage(message msg) {             switch (msg.what) {                 case filesender.connecting:                    // toast.maketext(senderactivity.this, "connecting...", toast.length_short).show();                     break;                  case filesender.connected:                    // toast.maketext(senderactivity.this, "connected!", toast.length_short).show();                     dialog.show();                     break;                  case filesender.sending_file:                   //  toast.maketext(senderactivity.this, "sending file!", toast.length_short).show();                   // mprogressdialog.show();                     break;                  case filesender.file_sent:                     //toast.maketext(senderactivity.this, "file sent!", toast.length_short).show();                     //mprogressdialog.dismiss();                     dialog.dismiss();                     filesender.close();                     bpickfile.setenabled(true);                     break;                  case filesender.send_error:                     //mprogressdialog.dismiss();                     dialog.dismiss();                     toast.maketext(senderactivity.this, "error occured : " + (string) msg.obj, toast.length_short).show();                     filesender.close();                     bpickfile.setenabled(true);                     break;             }         }     };       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_sender);          dialog = new spotsdialog(this, "sending file");         // creating our own toolbar          toolbar toolbar = (toolbar)findviewbyid(r.id.tb1);         setsupportactionbar(toolbar);         getsupportactionbar().settitle("data sharing");         toolbar.setsubtitle("northern university nowshera");         toolbar.setlogo(android.r.drawable.ic_menu_gallery);     // progress dialog          mprogressdialog = new progressdialog(senderactivity.this);         mprogressdialog.setcancelable(false);         mprogressdialog.setprogressstyle(progressdialog.style_spinner);         mprogressdialog.setmessage("sending file please wait ..");          bpickfile = (button) findviewbyid(r.id.bpickfile);          bti = (button)findviewbyid(r.id.imgbtn);         bti.setonclicklistener(new onclicklistener() {             @override             public void onclick(view view) {                 redirect();             }         });     }       public  void redirect()     {         intent in = new intent(this, selection_activity.class);         startactivity(in);     }       private void sendmyfile(string filepath) {          log.i("senderactivity", "file path : " + filepath);         toast.maketext(this, filepath, toast.length_short).show();          file file = new file(filepath);          filesender = new filesender(this, mhandler);          // if(!etcode.gettext().tostring().equals("")){         //   int code = integer.parseint(etcode.gettext().tostring());         // filesender.sendfile(file,code);         //    }         int code = 40500; //port no          filesender.sendfile(file, code);       }      public void pickafile(view view) {           intent intent = new intent(intent.action_get_content);         intent.settype("*/*");         intent.addcategory(intent.category_openable);          try {             startactivityforresult(                     intent.createchooser(intent, "select file upload"),                     file_select_code);         } catch (android.content.activitynotfoundexception ex) {             // potentially direct user market dialog             toast.maketext(this, "please install file manager.",                     toast.length_short).show();         }      }     public static string getpath(context context, uri uri) throws urisyntaxexception {         if ("content".equalsignorecase(uri.getscheme())) {             string[] projection = { "_data" };             cursor cursor = null;              try {                 cursor = context.getcontentresolver().query(uri, projection, null, null, null);                 int column_index = cursor.getcolumnindexorthrow("_data");                 if (cursor.movetofirst()) {                     return cursor.getstring(column_index);                 }             } catch (exception e) {                 // eat             }         }         else if ("file".equalsignorecase(uri.getscheme())) {             return uri.getpath();         }          return null;     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         switch (requestcode) {             case file_select_code:                 if (resultcode == result_ok) {                     // uri of selected file                     try {                         uri uri = data.getdata();                         log.d(tag, "file uri: " + uri.tostring());                         // path                         string path = fileutils.getpath(this, uri);                         log.d(tag, "file path: " + path);                         docpaths=new arraylist<>();                         docpaths.add(path);                         sendmyfile(docpaths.get(0));                     }                     catch (urisyntaxexception e) {                         e.printstacktrace();                     }                      // initiate upload                 }                 break;         }      } } 

file sender class

public class filesender {      private final boolean mdebug = true;      private final string port = "port";     private final string messenger = "messenger";     private final string file = "file";     private final string receiver_ip = "receiver_ip";       public static final int connecting = 1001;    public static final int connected = 1002;     public static final int sending_file = 1003;     public static final int file_sent = 1004;     public static final int send_error = 1005;      private context context;     private int port;     private handler mhandler;      private final wifimanager manager;     private final dhcpinfo dhcp;      intent i;      public filesender(context context, handler mhandler) {         this.context = context;         this.mhandler = mhandler;         manager = (wifimanager) context.getsystemservice(context.wifi_service);         dhcp = manager.getdhcpinfo();     }      private inetaddress getmyip() {         final string address = formatter.formatipaddress(dhcp.ipaddress); // ipaddress - ip address of device, assigned through dhcp         inetaddress myip = null;          try {              myip = inetaddress.getbyname(address);             if(mdebug)                 log.i("filesender","my ip : " + myip.tostring());          } catch (exception e) {             if(mdebug)                 log.e("filesender","cannot find own ip. error : " + e.tostring());         }          return myip;     }      private inetaddress getreceiverip() {         final string address = formatter.formatipaddress(dhcp.gateway); // gateway - default gateway ip address         inetaddress receiverip = null;          try {              receiverip = inetaddress.getbyname(address);              if(mdebug)                 log.i("filesender","receiver ip : " + receiverip.tostring());          } catch (exception e) {             if(mdebug)                 log.e("filesender","cannot find receiver's ip. error : " + e.tostring());         }          return receiverip;     }      public void sendfile(file file,int code) {          if(!file.exists()){             mhandler.obtainmessage(send_error,"file " + file.getname() + " doesn't exist").sendtotarget();             return;         }          if(!file.isfile()){             mhandler.obtainmessage(send_error,file.getname() + " folder, not file").sendtotarget();             return;         }          this.port = code;          inetaddress receiverip = getreceiverip();          = new intent(context,senderservice.class);          i.putextra(receiver_ip,receiverip);         i.putextra(port,port);         i.putextra(messenger,new messenger(mhandler));         i.putextra(file,file);           context.startservice(i);     }      public void close() {         if(context!=null && i!=null)             context.stopservice(i);     } } 

sender service

public class senderservice extends service {      private final string port = "port";     private final string messenger = "messenger";     private final string file = "file";     private final string receiver_ip = "receiver_ip";      private inetaddress receiverip;     private int port;     private messenger messenger;     private file file;      @override     public int onstartcommand(intent intent, int flags, int startid) {          bundle b = intent.getextras();          receiverip = (inetaddress) b.get(receiver_ip);         port = (int) b.get(port);         messenger = (messenger) b.get(messenger);         file = (file) b.get(file);          senderthread senderthread = new senderthread(receiverip,port,file,messenger);          senderthread.start();          return start_redeliver_intent;     }       @override     public ibinder onbind(intent intent) {         return null;     } } 

sender thread

public class senderthread extends thread {      private inetaddress receiverip;     private int port;     private file filetosend;     private messenger messenger;     private socket sendersocket;     private int pkt_size = 60*1024;      public senderthread(inetaddress receiverip, int port, file filetosend, messenger messenger){         this.receiverip = receiverip;         this.port = port;         this.messenger = messenger;         this.filetosend = filetosend;     }      @override     public void run() {         message message;          try {              message = message.obtain();             message.what = filesender.connecting;             message.obj = "";             messenger.send(message);              sendersocket = new socket(receiverip,port);              message = message.obtain();             message.what = filesender.connected;             message.obj = "";             messenger.send(message);              dataoutputstream out = new dataoutputstream(sendersocket.getoutputstream());              message = message.obtain();             message.what = filesender.sending_file;             message.obj = "";             messenger.send(message);              // send file name             out.writeutf(filetosend.getname());              datainputstream din = new datainputstream(new fileinputstream(filetosend));              // sent file size             long filesize = filetosend.length();             out.writelong(filesize);              int totallength = 0;             int length = 0;             byte[] senddata = new byte[pkt_size];              long starttime = system.currenttimemillis();              // send file data             while ((length = din.read(senddata)) != -1) {                  out.write(senddata, 0, length);                  totallength += length;              }              long stoptime = system.currenttimemillis();              din.close();              double time = (stoptime - starttime) / 1000.0;              double speed = (totallength / time) / 1048576.0;              message = message.obtain();             message.what = filesender.file_sent;             message.obj = "";              messenger.send(message);          } catch (exception e){              e.printstacktrace();              message = message.obtain();             message.what = filesender.send_error;             message.obj = e.tostring();              try {                 messenger.send(message);             } catch (remoteexception re) {                 log.e("senderthread","error in sending error message! error : " + re.tostring());                 re.printstacktrace();             }          } {              try {                  if(sendersocket!=null)                     sendersocket.close();              } catch (ioexception ioe) {                 log.e("senderthread","error in closing sockets. error : " + ioe.tostring());                 ioe.printstacktrace();             }         }     } } 


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -