In this video it shows the code to download a JPG file from FTP server and then display it over an ImageView in your Android App.
It uses following dependency in the code:
https://mvnrepository.com/artifact/commons-net/commons-net/3.10.0
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.downloadftpfile;
import static android.Manifest.permission.READ_MEDIA_IMAGES;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassWord, editTextFileName;
private TextView textViewStatus;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
ActivityCompat.requestPermissions(this,
new String[]{READ_MEDIA_IMAGES},
PackageManager.PERMISSION_GRANTED);
editTextFileName = findViewById(R.id.editTextFileName);
editTextPassWord = findViewById(R.id.editTextTextPassword);
editTextUserName = findViewById(R.id.editTextUserName);
textViewStatus = findViewById(R.id.textView);
imageView = findViewById(R.id.imageView);
StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(threadPolicy);
return insets;
});
}
public void buttonDownloadFTPFile(View view){
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // 0 for internal storage
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("192.168.1.1");
ftpClient.login(editTextUserName.getText().toString(),
editTextPassWord.getText().toString());
ftpClient.changeWorkingDirectory("usb1_1/Files/");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.sendCommand("OPTS UTF8 ON");
File fileImageLocal = new File(storageVolume.getDirectory().getPath() +
"/Download/" + System.currentTimeMillis() + ".jpg");
FileOutputStream fileOutputStream = new FileOutputStream(fileImageLocal);
boolean booleanStatus = ftpClient.retrieveFile(editTextFileName.getText().toString() + ".jpg",
fileOutputStream);
String stringText = "Downloaded File - " + String.valueOf(booleanStatus);
textViewStatus.setText(stringText);
fileOutputStream.flush();
fileOutputStream.close();
ftpClient.logout();
ftpClient.disconnect();
Bitmap bitmapImage = BitmapFactory.decodeFile(fileImageLocal.getPath());
imageView.setImageBitmap(bitmapImage);
} catch (Exception e) {
textViewStatus.setText(e.toString());
}
}
}
plugins {
alias(libs.plugins.androidApplication)
}
android {
namespace = "com.programmerworld.downloadftpfile"
compileSdk = 34
defaultConfig {
applicationId = "com.programmerworld.downloadftpfile"
minSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
// https://mvnrepository.com/artifact/commons-net/commons-net
implementation(libs.commons.net)
}
[versions]
agp = "8.3.1"
commonsNet = "3.10.0"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.11.0"
activity = "1.8.0"
constraintlayout = "2.1.4"
[libraries]
commons-net = { module = "commons-net:commons-net", version.ref = "commonsNet" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DownloadFTPFile"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FTP Download in Process"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.448" />
<EditText
android:id="@+id/editTextUserName"
android:layout_width="211dp"
android:layout_height="56dp"
android:layout_marginStart="100dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="text"
android:text="ftpadmin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="212dp"
android:layout_height="54dp"
android:layout_marginStart="100dp"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="Password ... "
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextUserName" />
<EditText
android:id="@+id/editTextFileName"
android:layout_width="211dp"
android:layout_height="61dp"
android:layout_marginStart="100dp"
android:layout_marginTop="11dp"
android:ems="10"
android:inputType="text"
android:text="flower"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_marginTop="20dp"
android:onClick="buttonDownloadFTPFile"
android:text="Download FTP File"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextFileName" />
<ImageView
android:id="@+id/imageView"
android:layout_width="352dp"
android:layout_height="237dp"
android:layout_marginStart="35dp"
android:layout_marginTop="23dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots:
FTP files:
Excerpt:
The provided content includes a video demonstrating the process of downloading a JPG file from an FTP server and displaying it on an ImageView in an Android app. The code uses the ‘commons-net’ dependency and provides contact information for further inquiries or feedback. Additionally, it includes the complete source code for the Android app, along with screenshots of the FTP files.