Search This Blog

Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

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


Saturday, 1 June 2013

Android action bar style generator

Action bar becomes popular after it launched in android 3.1 version. Using action with Fragment avoid overhead of maintaining stack of activity. But customizing action bar style is pain. We should thank to Jeff Gilfelt  for his great work. He provide tool of customizing action in unique style.
Just customize action bar style like editing a photo in Photo Editor and it will provide you complete resource with layout, style and draw-able (which include hdpi, ldpi and xhdpi images)

You can use Sherlock Action bar also if you have backward compatibility issue. You can customize its look and feel.

It provide you complete control on customization on android action bar style. you can change action bar Tab bottom line and all other thing related to it

Here Go for Action Bar Style Customization URL
Android News and source code