Search This Blog

Tuesday 18 February 2014

Gujarati language font support android example with source code

I had seen many developer struggling to support Guajarati language with android phone in their own application. Application like news hunt support Guajarati so that’s means its surely possible.

  • See very simple code to support Guajarati language inside android application. 

           TextView mtxt = (TextView) findViewById(R.id.action_settings); 

           Typeface face1 = Typeface.createFromAsset(getAssets(), 

                     "Lohit-Gujarati.ttf"); 

           mtxt.setTypeface(face1); 

           mtxt.setText("પૂર્વ વડાપ્રધાન રાજીવ ગાંધીની હત્યા કરનારા 3 આરોપીઓની ફાંસીની સજા સુપ્રિમ કોર્ટે ઉમ્રકેદમાં ફેરવી નાંખી છે.આમ રાજીવગાંધીના" + 

                     "3 હત્યારાઓને હવે ફાંસીની સજા નહી થાય.સુપ્રીમ કોર્ટમાં 3 જજોની બેન્ચે આજે આ ચુકાદો આ"); 




Download source Code

Monday 17 February 2014

Android FTP client tutorial with example of uploading, downloading and authentication with FTP server

FTP protocol is standard network protocol for transferring file. It works on peer to peer base network. In recent days I had written few articles about file transfer protocol and comparison among them to choose best. After research, research shows that FTP outperformed everything in android. So I decide to write about FTP client implementation. This tutorial will teach you
  • Android FTP client Authentication - How to connect with FTP server?
  • Android FTP client Download - How to download a file from FTP server?
  • Android FTP client Upload -How to upload a file to FTP server?

Basic requirement for this tutorial is one simple FTP server setup at your desktop. You can use file-Zilla or IIS server for this requirement.

  • Android FTP client authentication and listing file from FTP server

      /**  
       *   
       * @param ip  
       * @param userName  
       * @param pass  
       */  
      public void connnectingwithFTP(String ip, String userName, String pass) {  
           boolean status = false;  
           try {  
                FTPClient mFtpClient = new FTPClient();  
                mFtpClient.setConnectTimeout(10 * 1000);  
                mFtpClient.connect(InetAddress.getByName(ip));  
                status = mFtpClient.login(userName, pass);  
                Log.e("isFTPConnected", String.valueOf(status));  
                if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {  
                     mFtpClient.setFileType(FTP.ASCII_FILE_TYPE);  
                     mFtpClient.enterLocalPassiveMode();  
                     FTPFile[] mFileArray = mFtpClient.listFiles();  
                     Log.e("Size", String.valueOf(mFileArray.length));  
                }  
           } catch (SocketException e) {  
                e.printStackTrace();  
           } catch (UnknownHostException e) {  
                e.printStackTrace();  
           } catch (IOException e) {  
                e.printStackTrace();  
           }  
      }  

  • Android FTP client download a file from FTP server – you can browse through directory saved on FTP server and can download desired file or directory. I just write code here for downloading a single file and writing it to sdcard/phone memory.

      /**  
       * @param ftpClient FTPclient object  
       * @param remoteFilePath  FTP server file path  
       * @param downloadFile   local file path where you want to save after download  
       * @return status of downloaded file  
       */  
      public boolean downloadSingleFile(FTPClient ftpClient,  
                String remoteFilePath, File downloadFile) {  
           File parentDir = downloadFile.getParentFile();  
           if (!parentDir.exists())  
                parentDir.mkdir();  
           OutputStream outputStream = null;  
           try {  
                outputStream = new BufferedOutputStream(new FileOutputStream(  
                          downloadFile));  
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
                return ftpClient.retrieveFile(remoteFilePath, outputStream);  
           } catch (Exception ex) {  
                ex.printStackTrace();  
           } finally {  
                if (outputStream != null) {  
                     try {  
                          outputStream.close();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                }  
           }  
           return false;  
      }  

For browsing through directory see this Browsing Nested Directory

  • Android FTP client uploading a file to FTP server – You can upload a file to server with object of FTPClient at desired path which you need to define.

      /**  
       *   
       * @param ftpClient FTPclient object  
       * @param downloadFile local file which need to be uploaded.  
       */  
      public void uploadFile(FTPClient ftpClient, File downloadFile,String serverfilePath) {  
           try {  
                FileInputStream srcFileStream = new FileInputStream(downloadFile);  
                boolean status = ftpClient.storeFile("remote ftp path",  
                          srcFileStream);  
                Log.e("Status", String.valueOf(status));  
                srcFileStream.close();  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  

Download Jar  

 

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

 See Sambha file sharing client for android

Wednesday 12 February 2014

Best file transfer protocol : Which to use among SMB, Socket (TCP/UDP) and FTP?



I had gone through the requirement of file transfer using peer to peer in a same network. I tried the case of server and client too because I had not any issue to run server script (in case of Socket Connection). Main purpose was to attain maximum transfer speed of data. So I tried with three most popular protocols for file transfers which are –
  • SMB – Server Message Protocols known also as sambha file sharing
  • Socket- Work on the base of client server concept
  • FTP- File transfer protocol

Comparison for performance –


Phone
Protocol
Data
Distance
Time
Max Speed
XOLO
Socket
1.3 GB
5 Meter
16 Min
1.3 MB/S
XOLO
SMB
0.7 GB
5 Meter
25 Min
0.5 MB/S
XOLO
FTP
1.3 GB
5 Meter
14 Min
1.6 MB/S

Notable Point about file transfer protocols –

  • Socket – Server scripting required for file transfer. This connection cannot be named as peer to peer
  • SMB – No scripting required for file transfer. It can read any file structure of peer computer
  • FTP FileZilla can do the trick for file transfer. No Server script required

Term and Condition – These all testing has been done between an android and a low configuration desktop (Windows 7). Both were connected to one dedicated wifi local network



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


Android News and source code