How can I upload a picture taken with my camera?

2019-03-03 16:18发布

问题:

I'm trying to send pictures that I have taken with my camera to my server, via FTP. I've already coded the portion of the program responsible for taking the picture. But the next step is to send the picture to my FTP server. How would I go about doing this?

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();
        }
    }
}

Here is some code that I found from the question How do you upload images to an FTP server within an Android app?. Where in my program would I put the following code?

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();

Or is there a better way that I am not aware of to upload my picture to a FTP server.