2016-09-17 55 views
1

我正在開發一個項目,我需要拍攝一張圖片位圖並將其上傳到服務器,而我無法將其包圍。Android將位圖上傳到服務器PNG

這是我現在使用什麼(忽略的JSONObject的東西,服務器返回的響應,我還沒有建成的那件,只是還沒有):

private JSONObject uploadImagesFromMemory(Bitmap bitmap) { 
    if (getSelf() == null) { 
     try { 
      return new JSONObject(""); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    HttpURLConnection connection = null; 

    try { 
     URL url = new URL("REMOVED"); 
     connection = (HttpURLConnection) url.openConnection(); 

     connection.setDoOutput(true); 
     connection.setUseCaches(false); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Content-Type", "multipart/form-data;"); 

     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     OutputStream outputStream = connection.getOutputStream(); 
     outputStream.write(byteArray); 

     outputStream.flush(); 
     outputStream.close(); 
     connection.disconnect(); 

     return new JSONObject("{\"responseCode:\"1}"); 
    } catch (Exception ex) { 
     //Exception handling 
    } 

    return null; 
} 

我不知道該怎麼將文件的上傳名稱/變量設置爲服務器要求的「toUpload」。

會採取任何建議,謝謝。

回答

1

我改變了我的代碼相當多的(服務器和客戶端)和它的工作現在。

服務器僅取得Base64數據並將其轉換爲PNG文件。然後它做什麼它以前那樣,並將其保存:

if (isset($_POST['base64'])) { 
$data = $_POST['base64']; 
$data = base64_decode($data); 

$status = file_put_contents("profiles/$uuid.png", $data); 

if ($status == false) { 
    echo(json_encode(array(
     "response" => "Failed to change profile picture.", 
     "responseCode" => "0" 
    ))); 
} else { 
    echo(json_encode(array(
     "response" => "Successfully changed your profile picture!", 
     "responseCode" => "1" 
    ))); 
} 

}

客戶需要的位圖,並將其轉換爲Base64編碼:

public String toBase64(Bitmap bitmap) { 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream .toByteArray(); 
    return Base64.encodeToString(byteArray, Base64.DEFAULT); 
} 

然後,用排槍,它上傳它:

final ProgressDialog loading = ProgressDialog.show(SettingsActivity.this,"Uploading...","Please wait...",false,false); 
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "REMOVED", 
          new Response.Listener<String>() { 
           @Override 
           public void onResponse(String s) { 
            //Dismissing the progress dialog 
            loading.dismiss(); 
            //Showing toast message of the response 
            try { 
             JSONObject jsonObject = new JSONObject(s); 
             int responseCode = Integer.parseInt(jsonObject.getString("responseCode")); 
             String response = jsonObject.getString("response"); 
             if (responseCode == 1) { 
              Toast.makeText(SettingsActivity.this, response, Toast.LENGTH_LONG).show(); 
             } else { 
              Toast.makeText(SettingsActivity.this, "Error: " + response, Toast.LENGTH_LONG).show(); 
             } 
            } catch (Exception ex) { 
             Toast.makeText(SettingsActivity.this, "Failed to upload.", Toast.LENGTH_LONG).show(); 
            } 
           } 
          }, 
          new Response.ErrorListener() { 
           @Override 
           public void onErrorResponse(VolleyError volleyError) { 
            //Dismissing the progress dialog 
            loading.dismiss(); 

            //Showing toast 
            try { 
             JSONObject jsonObject = new JSONObject(volleyError.getMessage()); 
             int responseCode = Integer.parseInt(jsonObject.getString("responseCode")); 
             String response = jsonObject.getString("response"); 
             if (responseCode == 1) { 
              Toast.makeText(SettingsActivity.this, response, Toast.LENGTH_LONG).show(); 
             } else { 
              Toast.makeText(SettingsActivity.this, "Error: " + response, Toast.LENGTH_LONG).show(); 
             } 
            } catch (Exception ex) { 
             Toast.makeText(SettingsActivity.this, "Failed to upload.", Toast.LENGTH_LONG).show(); 
            } 
           } 
          }){ 
         @Override 
         protected Map<String, String> getParams() throws AuthFailureError { 
          //Converting Bitmap to String 
          String image = Memer.getMemer().toBase64(bitmap); 

          //Getting Image Name 

          //Creating parameters 
          Map<String,String> params = new Hashtable<>(); 

          //Adding parameters 
          params.put("base64", image); 

          //returning parameters 
          return params; 
         } 
        }; 

        //Creating a Request Queue 
        RequestQueue requestQueue = Volley.newRequestQueue(SettingsActivity.this); 

        //Adding request to the queue 
        requestQueue.add(stringRequest); 

希望我能幫助其他人像我這樣的問題奮鬥:)

0

我發現最簡單的事情就是使用MultipartEntityBuilder(需要使用apache庫)。使用該代碼是:

 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
     builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     for (Map.Entry<String, File> entry : entities.entrySet()) 
     { 
      builder.addBinaryBody(entry.getKey(), entry.getValue(), ContentType.create("image/jpg"), entry.getValue().getName()); 
     } 
     mMultipartEntity = builder.build(); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     try { 
      mMultipartEntity.writeTo(bos); 
     } 
     catch (IOException ex) { 
     } 
     return bos.toByteArray(); 

的URLConnection的庫,Android的切換是偉大的簡單的查詢,但似乎並沒有像樣multupart實體的支持。

0
如果你修改你的代碼

因此這將幫助你

FileInputStream fileInputStream = new FileInputStream(sourceFile); 
       URL url = new URL(upLoadServerUri+"&user_id="+userId); 
       // Open a HTTP connection to the URL 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setDoInput(true); // Allow Inputs 
       conn.setDoOutput(true); // Allow Outputs 
       conn.setUseCaches(false); // Don't use a Cached Copy 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
       conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); 
       conn.setRequestProperty("uploaded_file", fileName); 

       //conn.setRequestProperty("uname", username); 
       //conn.setRequestProperty("uid", userId); 
       dos = new DataOutputStream(conn.getOutputStream()); 
       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name=\"edit_pro_pic\";filename=\"" + fileName + "\"" + lineEnd); 
       dos.writeBytes(lineEnd); 
       // create a buffer of maximum size 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       buffer = new byte[bufferSize]; 
       // read file and write it into form... 
       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); 
       } 
       // send multipart form data necesssary after file data. 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
       // Responses from the server (code and message) 
       serverResponseCode = conn.getResponseCode(); 
       conn.getResponseMessage(); 
       s = new Scanner(conn.getInputStream()); 
       imageUrl = readResponse(s.next()); 
       // close the streams // 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 
      } catch (MalformedURLException ex) { 
       dialog.dismiss(); 
       cancel(true); 
       ex.printStackTrace(); 
       //Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
      } catch (Exception e) { 
       dialog.dismiss(); 
       e.printStackTrace(); 
       // Log.e("Upload file to server Exception","Exception : " + e.getMessage(), e); 
      }