Android senden von Bild-zu-Server über MultipartEntity - Einstellung " Content-type?

Gelesen habe ich viele, viele Beiträge über die Zusendung eines Bildes an den server von einem Android-app und Content-type-Weise, Sie sind in drei Kategorien unterteilt:

a) Sie nicht setzen den content-type und wohl irgendwie Ihr code arbeitet,

b) Sie sind mit überholten Methoden

c) Sie sind mit völlig anderen Ansatz aus, den ich ausgewählt habe.

Ich würde gerne senden Sie die Datei auf den server und speichern Sie in einem Ordner.

Mein code ist insgesamt patchwork, ein Metzgerei-job, den ich konnte nach der Lektüre viele Beiträge und Artikel, hier ist es:

public void uploadImageToServer(String imagePath) throws Exception {

    try {

        //set the http handlers
        httpClient = new DefaultHttpClient();
        localContext = new BasicHttpContext(); //why do I need this?
        postRequest = new HttpPost("http://asd.com/asd.php"); 
        //postRequest.addHeader("Content-type", "image/jpeg"); //- this didnt work

        //deal with the file
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap = BitmapFactory.decodeFile(imagePath);
        bitmap.compress(CompressFormat.JPEG, 75, byteArrayOutputStream); 
        byte[] byteData = byteArrayOutputStream.toByteArray();
        //String strData = Base64.encodeToString(data, Base64.DEFAULT); //I have no idea why Im doing this
        ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); //second parameter is the name of the image (//TODO HOW DO I MAKE IT USE THE IMAGE FILENAME?)

        //send the package
        multipartEntity = MultipartEntityBuilder.create();
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("uploaded_file", byteArrayBody);

        postRequest.setEntity(multipartEntity.build());

        //get the response. we will deal with it in onPostExecute.
        response = httpClient.execute(postRequest, localContext);
        bitmap.recycle();
    } catch (Exception e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Und der Fehler ist:

 FATAL EXCEPTION: AsyncTask #1
 java.lang.RuntimeException: An error occured while executing doInBackground()
android.os.AsyncTask$3.done(AsyncTask.java:200)
 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
java.util.concurrent.FutureTask.setException(FutureTask.java:125)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
 org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:67)
 org.apache.http.entity.mime.content.ByteArrayBody.<init>(ByteArrayBody.java:87)
  • Nur Überprüfen Sie Ihre Server-Seite, wenn das Bild in .png oder im .jpg-format??
InformationsquelleAutor J. K. | 2013-12-27
Schreibe einen Kommentar