Android: Caricamento di immagini sul server con php

Caricare sempre image test.jpg con 0KB sul server!

Ecco l'image:

  • Rileva l'inizio di un suono o una voce in Android
  • Qual è l'equivalente di "colspan" in un Android TableLayout?
  • come utilizzare traceview in eclipse per lo sviluppo di android?
  • Creazione di un nuovo field AVD-CPU / ABI mostra 'Nessuna image di sistema installata'
  • Modificare il colore di background della scheda e rimuovere la row di divisione tra le tabs
  • Cambiare l'impostazione della luce di segnalazione a impulsi / trackball su Android (solo Nexus One)
  • http://s11.postimage.org/xz6gbi1rl/Capture_Problem_Android.jpg

    Quindi puoi vedere cosa sto accendendo sul server.

    Vorrei apprezzare qualsiasi aiuto o suggerire e spero che risolverò il problema ..

    Codice Android:

    public class Main extends Activity { InputStream inputStream; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. byte [] byte_arr = stream.toByteArray(); String image_str = Base64.encodeBytes(byte_arr); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("image",image_str)); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://server/uploader2.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); String the_string_response = convertResponseToString(response); Toast.makeText(this, "Response " + the_string_response, Toast.LENGTH_LONG).show(); }catch(Exception e){ Toast.makeText(this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); System.out.println("Error in http connection "+e.toString()); } } public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ String res = ""; StringBuffer buffer = new StringBuffer(); inputStream = response.getEntity().getContent(); int contentLength = (int) response.getEntity().getContentLength(); //getting content length….. Toast.makeText(this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show(); if (contentLength < 0){ } else{ byte[] data = new byte[512]; int len = 0; try { while (-1 != (len = inputStream.read(data)) ) { buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer….. } } catch (IOException e) { e.printStackTrace(); } try { inputStream.close(); // closing the stream….. } catch (IOException e) { e.printStackTrace(); } res = buffer.toString(); // converting stringbuffer to string….. Toast.makeText(this, "Result : " + res, Toast.LENGTH_LONG).show(); //System.out.println("Response => " + EntityUtils.toString(response.getEntity())); } return res; }} 

    e il codice php … (uploader2.php)

     <?php $base=$_REQUEST['image']; $binary=base64_decode($base); header('Content-Type: image/jpg; charset=utf-8'); $file = fopen('test.jpg', 'wb'); fwrite($file, $binary); fclose($file); echo 'Image upload complete!!, Please check your php file directory……'; echo "<img src=test.jpg>"; ?> 

    qual è il problema?

  • Copertura del codice Jacoco per l'applicazione Android usando la libreria mockito
  • Android ListView con elementi cliccabili nelle sue righe causa problemi a continuare a scorrere
  • Come posso hide la tastiera Android utilizzando JavaScript?
  • Come convertire MatOfPoint in MatOfPoint2f in opencv java api
  • MvxListView crea un collegamento per il layout di un model dal codice
  • Differenti formati di caratteri per diverse size dello schermo
  • One Solution collect form web for “Android: Caricamento di immagini sul server con php”

    Funzione doFileUpload:

     private void doFileUpload(){ HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null; String exsistingFileName = "/sdcard/six.3gp"; // Is this the place are you doing something wrong. String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; String urlString = "http://192.168.1.5/upload.php"; try { Log.e("MediaPlayer","Inside second Method"); FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) ); URL url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); dos = new DataOutputStream( conn.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd); dos.writeBytes(lineEnd); Log.e("MediaPlayer","Headers are written"); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) tv.append(inputLine); // close streams Log.e("MediaPlayer","File is written"); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformsdURLException ex) { Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); } //------------------ read the SERVER RESPONSE try { inStream = new DataInputStream ( conn.getInputStream() ); String str; while (( str = inStream.readLine()) != null) { Log.e("MediaPlayer","Server Response"+str); } /*while((str = inStream.readLine()) !=null ){ }*/ inStream.close(); } catch (IOException ioex){ Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); } } 

    upload.php

     <?php move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "./upload/".$_FILES["uploadedfile"]["name"]); mysql_connect("localhost","root",""); mysql_select_db("chat"); if(isset($_REQUEST['msg'])) { $a = $_REQUEST['msg']; $sql = "INSERT INTO upload(image)VALUES('$a')"; mysql_query($sql); } ?> 
    L'Android è un fan Android di Google, tutto su telefoni Android, Android Wear, Android Dev e applicazioni Android Games e così via.