Ho un'applicazione che carica foto tramite un servizio web. In passato, ho caricato un file in un flusso e convertito in Base64. Quindi ho postato la stringa risultante attraverso il metodo write() di un OutputStreamWriter. Ora, il servizio web è cambiato, e si aspetta dati multipart/form e non si aspetta Base64.Android - Scrive array di byte su OutputStreamWriter
Quindi, in qualche modo, ho bisogno di pubblicare i dati di questo file così com'è senza conversione. Sono sicuro di essere vicino, ma tutto ciò che ottengo è una durata del contenuto in underflow o overflow. La cosa strana è che nel debugger posso vedere che la lunghezza del mio buffer è della stessa lunghezza della stringa che sto postando. Ecco quello che sto facendo e basta, auspicabilmente, il codice:
// conn is my connection
OutputStreamWriter dataStream = new OutputStreamWriter(conn.getOutputStream());
// c is my file
int bytesRead = 0;
long bytesAvailable = c.length();
while (bytesAvailable > 0) {
byte[] buffer = new byte[Math.min(12288, (int)bytesAvailable)];
bytesRead = fileInputStream.read(buffer, 0, Math.min(12288, (int)bytesAvailable));
// assign the string if needed.
if (bytesRead > 0) {
bytesAvailable = fileInputStream.available();
// I've tried many encoding types here.
String sTmp = new String(buffer, "ISO-8859-1");
// HERE'S the issue. I can't just write the buffer,
dataStream.write(sTmp);
dataStream.flush();
// Yes there's more code, but this should be enough to show why I don't know what I'm doing!
C'è qualche motivo per cui si convertono i byte in stringhe invece di scriverli in modo raw nello stream di output? – Michael
Poiché il metodo write() di OutputStreamWriter accetta char [] o string. Non byte []. – Paul
Provare a utilizzare un 'DataOutputStream'. –