使用Android应用程序将图片从设备上传到FTP服务器可以通过以下步骤实现:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<Button
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:scaleType="centerCrop" />
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private static final int REQUEST_IMAGE_SELECT = 3;
private Button selectButton;
private ImageView imageView;
private String ftpServer = "ftp.example.com";
private int ftpPort = 21;
private String ftpUsername = "username";
private String ftpPassword = "password";
private String ftpRemoteDir = "/upload/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectButton = findViewById(R.id.selectButton);
imageView = findViewById(R.id.imageView);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
} else {
selectImage();
}
}
});
}
private void selectImage() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE_SELECT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_SELECT) {
Uri selectedImageUri = data.getData();
String imagePath = getPathFromUri(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);
uploadImage(imagePath);
}
}
}
private String getPathFromUri(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(columnIndex);
cursor.close();
return imagePath;
}
private void uploadImage(final String imagePath) {
new Thread(new Runnable() {
@Override
public void run() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ftpServer, ftpPort);
ftpClient.login(ftpUsername, ftpPassword);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
File file = new File(imagePath);
BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(file));
ftpClient.storeFile(ftpRemoteDir + file.getName(), buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Image uploaded successfully", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Failed to upload image", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
}
以上代码使用了Apache Commons Net库来处理FTP连接和文件上传。请确保在项目的build.gradle文件中添加以下依赖项:
implementation 'commons-net:commons-net:3.8.0'
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这样,当用户点击"Select Image"按钮时,将打开设备的图库,用户可以选择要上传的图片。选择后,图片将显示在ImageView中,并通过FTP上传到指定的服务器目录。
这是一个基本的Android应用程序,用于将图片从设备上传到FTP服务器。根据实际需求,可以进一步优化和扩展该应用程序,例如添加进度条、错误处理等功能。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云