Using apache commons Ftp library in Android

1) Download commons-net-2.0-src files

Download the source files commons-net-2.0-src.zip or commons-net-2.0-src.tar.gz from http://commons.apache.org/net/download_net.cgi. I tried with the binaries version containing only the jar files. But when i add the jar files to my Android project, I got the error: "Conversion to Dalvik format failed with error 1".


2) Extract and copy Ftp and dependent source files to Android project

Extract the compressed source files. The files will be extracted to commons-net-2.0-src folder. Go to commons-net-2.0-src | src | main | java | org | apache | commons | net folder. Copy all .java files in the net folder and the subfolders ftp, io, util. Am copying only files required for Ftp to work. If need other network protocols then copy the corresponding subfolders also.

In Android project's src folder, create the folders org | apache | commons | net and paste the files and subfolders copied above in the newly created net folder.


Here's how it looks like in eclipse package explorer:


Note: There may be other better ways to import external source files to an eclipse project. But am quite new to eclipse. So the first simplest way i could come up now is to just copy the source files needed into Android's src folder.



3) Using the Ftp library for file tranfer

You are now ready to use the classes available from apache commons Ftp library. Below is a very simple basic code example for uploading file from Android to an Ftp server. For more functionalities, refer to the api doc from apache commons project or just do a google search on how to use the Ftp classes from apache commons.


package laval.apacheftpclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import android.app.Activity;
import android.os.Bundle;

public class ApacheFTPClient extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        FTPClient mFTP = new FTPClient();
        try {
            // Connect to FTP Server
            mFTP.connect("192.168.1.110");
            mFTP.login("user", "password");
            mFTP.setFileType(FTP.BINARY_FILE_TYPE);
            mFTP.enterLocalPassiveMode();
            
            // Prepare file to be uploaded to FTP Server
            File file = new File("/path/to/filetotranfer");
            FileInputStream ifile = new FileInputStream(file);
            
            // Upload file to FTP Server
            mFTP.storeFile("filetotranfer",ifile);
            mFTP.disconnect();          
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Prevent Android from rotating your activity

There are applications in which you want to have your Android's activity to always display in landscape or portrait even if the user rotates the device. To do so, add the android:screenOrientation and android:configChanges attributes of your activity tag in the AndroidManifest.xml file.

Example to keep your activity to always display in portrait:

 1 <activity android:name=".AndroidApplication"
 2           android:label="@string/app_name" 
 3           android:screenOrientation="portrait" 
 4           android:configChanges="keyboardHidden|orientation">
 5     <intent-filter>
 6         <action android:name="android.intent.action.MAIN" />
 7         <category android:name="android.intent.category.LAUNCHER" />
 8     </intent-filter>
 9 </activity>


Line 3, android:screenOrientation="portrait", is telling Android not to rotate this activity but to always display it in portrait orientation. Android will still destroy and re-create this activity if the user rotates the device though it will always be re-created in portrait orientation.

Line 4, android:configChanges="keyboardHidden|orientation", prevents the activity from being recreated. It's telling Android that you will handle the keyboardHidden and orientation changes instead of letting Android handle these and would re-create the activity. If you want to do some processing on those events then you will have to implement the onConfigurationChanged() in your activity Java codes. Else like in my case where I want my activity to continue on what it was doing, just don't implement the onConfigurationChanged().

Note that these settings are for one activity and not the whole application. So if you need to keep orientation fixed for other activities, you will have to set those settings on each of the activities.