我正在尝试通过FTP将我用相机拍摄的照片发送到我的服务器。我已经编写了负责拍照的程序部分。但下一步是将图片发送到我的FTP服务器。我该怎么做呢?
package de.android.datenuebertragung;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Environment;
import android.util.Log;
public class KameraCallbacks implements ShutterCallback, PictureCallback {
private static final String TAG = KameraCallbacks.class.getSimpleName();
public void onShutter() {
Log.d(TAG, "onShutter()");
}
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken()");
// In welchem Verzeichnis soll die Datei abgelegt werden?
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// ggf. Verzeichnisse anlegen
dir.mkdirs();
// Name der Datei
File file = new File(dir, Long.toString(System.currentTimeMillis())
+ ".jpg");
FileOutputStream fos = null;
BufferedOutputStream bos = null;
// Datei gepuffert schreiben
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(data);
} catch (IOException e) {
Log.e(TAG, "onPictureTaken()", e);
} finally {
// Ströme schließen - etwaige Exceptions ignorieren
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
// Live-Vorschau neu starten
camera.startPreview();
}
}
}
以下是我在问题How do you upload images to an FTP server within an Android app?中找到的一些代码。在我的程序中,我会将以下代码放在哪里?
SimpleFTP ftp = new SimpleFTP();
// Connect to an FTP server on port 21.
ftp.connect("server address", 21, "username", "pwd");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("path");
// Upload some files.
ftp.stor(new File("your-file-path"));
// Quit from the FTP server.
ftp.disconnect();
或者有一种更好的方法,我不知道将我的照片上传到FTP服务器。