Search This Blog

Support my cause by downloading

Sunday, 9 February 2014

Android sambha/ JSIFS file sharing example and source code

Look at ES File explorer functionality of connecting to peer computer (either android or Desktop) and reading file structure of that device on your own device to copy content. I just implemented this functionality using SMB file sharing system in android. I will explain with source code and example. You can connect to any peer computer using its IP and password. It will allow you to read full directory of this computer. This process is called peer to peer connection.

  • Why to use Sambha File Sharing system

           For making file sharing system like ES Android file explorer which connect peer device on LAN

You will need IP and Password of that peer which you want to connect through SMB file transfer system.
Connecting android with peer using SMB file sharing -

      public void connectingWithSmbServer() {  
           try {  
                String yourPeerPassword = "administrator";  
                String yourPeerName = "abcd1234";  
                String yourPeerIP = "";  
                String path = "smb://" + yourPeerIP;  
                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(  
                          null, yourPeerName, yourPeerPassword);  
                Log.e("Connected", "Yes");  
                SmbFile smbFile = new SmbFile(path, auth);  
                /** Printing Information about SMB file which belong to your Peer **/  
                String nameoffile = smbFile.getName();  
                String pathoffile = smbFile.getPath();  
                Log.e(nameoffile, pathoffile);  
           } catch (Exception e) {  
                Log.e("Connected", e.getMessage());  

Once you connected you can browse through the file system easily and you can download any file from peer to android sdcards. Once see below code to download file from peer to android sdcard using SMB file sharing system -

      public void downloadFileFromPeerToSdcard(File mLocalFile, SmbFile mFile) {  
           try {  
                SmbFileInputStream mFStream = new SmbFileInputStream(mFile);  
                mLocalFile = new File(Environment.getExternalStorageDirectory(),  
                FileOutputStream mFileOutputStream = new FileOutputStream(  
                byte[] buffer = new byte[1024];  
                int len1 = 0;  
                while ((len1 = > 0) {  
                     mFileOutputStream.write(buffer, 0, len1);  
           } catch (MalformedURLException e) {  
                Log.e("MalformURL", e.getMessage());  
           } catch (SmbException e) {  
                Log.e("SMBException", e.getMessage());  
           } catch (Exception e) {  
                Log.e("Exception", e.getMessage());  

Note : Keep all methods inside background thread (i.e Asynchronous Task, Service)

You need to add one JSIFS Sambha jar file. Enjoy

Android News and source code