• Home
    ->
    Step by step guides
    ->
    SFTP Android app



    1. Computer or Laptop
    2. Android Studio – The official integrated development environment (IDE) for Google’s Android operating system.
    3. Knowledge of Java or Kotlin – These are the primary languages used for Android app development.
    4. Android Device or Emulator – for testing the app.
    5. SFTP Server – A location where you can upload your image. Make sure you have the server address, username and password.
    6. Image Gallery – Images that you will use to test the share and upload functionality.

    Once you have these ready, let’s start:

    Step 1: Create a New Android Studio Project
    Open Android Studio and create a new project with an Empty Activity template. Select Java or Kotlin as your language.

    Step 2: Add the necessary permissions
    In your app module’s AndroidManifest.xml, add the following permissions:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    Step 3: Design UI for Image Selection
    Open activity_main.xml and add an ImageView and Button. The ImageView will display the selected image and the Button will facilitate the sharing to the SFTP server. Here’s a basic template:

    <ImageView
        android:id="@+id/selectedImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="Selected image" />
    
    <Button
        android:id="@+id/buttonShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share" />

    Step 4: Select an Image From Gallery
    On clicking the ImageView, allow the user to pick an image from the gallery. You’ll need to create an Intent to open the gallery and get the image URI.

    selectedImageView.setOnClickListener(v -> {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    });

    Ensure you implement onActivityResult to get the selected image URI and set the image to the ImageView.

    Step 5: Implement the SFTP Upload
    Use an SFTP client library, like JSch. You’ll need to add it to your build.gradle dependencies:

    implementation 'com.jcraft:jsch:0.1.55'

    Implement the upload within an AsyncTask, as it’s a network operation and can’t be done on the UI thread.

    public class uploadToServer extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            JSch jsch = new JSch();
            Session session = null;
            try {
                session = jsch.getSession("[SFTP_USERNAME]", "[SERVER_ADDRESS]", 22);
                session.setConfig("StrictHostKeyChecking", "no");
                session.setPassword("[SFTP_PASSWORD]");
                session.connect();
    
                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp sftp = (ChannelSftp) channel;
    
                InputStream inputStream = getContentResolver().openInputStream(imageUri);
                sftp.put(inputStream, "/path/to/upload");
    
                sftp.disconnect();
                channel.disconnect();
                session.disconnect();
            } catch (JSchException | SftpException | FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    In the code above, you need to replace [SFTP_USERNAME], [SERVER_ADDRESS], and [SFTP_PASSWORD] with your actual server details.

    Step 6: Add onClickListener to the Button
    When you click the share button, execute the AsyncTask we created above.

    buttonShare.setOnClickListener(v -> {
        new uploadToServer().execute();
    });