Search This Blog

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 = "192.168.1.3";  
                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) {  
                e.printStackTrace();  
                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(),  
                          mFile.getName());  
                FileOutputStream mFileOutputStream = new FileOutputStream(  
                          mLocalFile);  
                byte[] buffer = new byte[1024];  
                int len1 = 0;  
                while ((len1 = mFStream.read(buffer)) > 0) {  
                     mFileOutputStream.write(buffer, 0, len1);  
                }  
                mFileOutputStream.close();  
                mFStream.close();  
           } catch (MalformedURLException e) {  
                e.printStackTrace();  
                Log.e("MalformURL", e.getMessage());  
           } catch (SmbException e) {  
                e.printStackTrace();  
                Log.e("SMBException", e.getMessage());  
           } catch (Exception e) {  
                e.printStackTrace();  
                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


Friday, 31 January 2014

Starting Developing application with android Studio is fun or Pain !

Android launched new android development environment  few months ago, is known as android studio based on Intelli J IDEA. This development tool is in early stage of preview. You can make android application using this tool too.

What android studio right away offer -

  •         Gradle-based build support.
  •         Android-specific refactoring and quick fixes.
  •         Lint tools to catch performance, usability, version compatibility and other problems.
  •         ProGuard and app-signing capabilities.
  •         Template-based wizards to create common Android designs and components.
  •         A rich layout editor that allows you to drag-and-drop UI components, preview layouts on  multiple screen configurations, and much more.
  •         Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine as server-side components.
See android developer guide installing and setup environment. Android Studio is a promising tool which will going to be more stable and useful in coming release. I just goes through google and find out benefits and constraint of Intelli J IDEA, Android studio

Benefits 


  •   All benefit which you read in first heading, will get you right away
  •  You can import your existing android project inside android studio
  •   Look modern compare to eclipse, more responsive  and integrated plugin like ADB and sdk Manager.

 

Should we go for it?

 

Yes go for it and learn it, but do not use fully as its in early stage of development and little unstable. With few more release you can directly start development. There is no place in which android studio lack compare to eclipse

 

See some screenshot of android studio while creating an new project


Home Android Studio

First Project Screen


Android News and source code