CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: gioviale1956 con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: bluemask con ben 8 Thanks ricevuti negli ultimi sette giorni
Utente del mese: carotix con ben 24 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 3 su 3
Discussione:

Inviare foto al server php

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Androidiano L'avatar di Vinz87


    Registrato dal
    Jun 2011
    Messaggi
    63

    Ringraziamenti
    3
    Ringraziato 3 volte in 3 Posts
    Predefinito

    Inviare foto al server php

    Salve, dovrei passare delle stringhe ed una foto tramite la mia app android, solo che non ho mai inviato come informazione una foto.
    Le stringhe le passo tranquillamente grazie all'httpget, ma non ho la minima idea di come inviare una foto, consigli? aiuti? frammenti di codice?
    Grazie
    Vincenzo

  2.  
  3. #2
    Senior Droid L'avatar di freebong


    Registrato dal
    Apr 2011
    Messaggi
    379

    Ringraziamenti
    13
    Ringraziato 28 volte in 27 Posts
    Predefinito

    Quote Originariamente inviato da Vinz87 Visualizza il messaggio
    Salve, dovrei passare delle stringhe ed una foto tramite la mia app android, solo che non ho mai inviato come informazione una foto.
    Le stringhe le passo tranquillamente grazie all'httpget, ma non ho la minima idea di come inviare una foto, consigli? aiuti? frammenti di codice?
    Grazie
    Potresti utilizzare qualcosa del genere:

    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = "/data/file_to_send.mp3";
    String urlServer = "http://192.168.1.1/handle_upload.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    serverResponseCode = connection.getResponseCode();
    serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
    //Exception handling
    }

    mentre sul server:

    <?php
    $target_path = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    ?>

  4. #3
    Androidiano L'avatar di Vinz87


    Registrato dal
    Jun 2011
    Messaggi
    63

    Ringraziamenti
    3
    Ringraziato 3 volte in 3 Posts
    Predefinito

    Grazie, proverò e ti farò sapere se risolvo ^^
    Vincenzo

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy