2016-12-28 15 views
0

私は関数を呼び出すボレーアンドロイドのサーバーにイメージをファイルとしてアップロードするには?

public class ImageSendJsonObjectHeader extends JsonRequest<JSONObject> { 


     /* public ImageSendJsonObjectHeader (int method, String url, String requestBody, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) { 
      super(method, url, requestBody, listener, errorListener); 
     } 
    */ 
     public ImageSendJsonObjectHeader (String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, 
           Response.ErrorListener errorListener) { 
      this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, 
        listener, errorListener); 
     } 

     public ImageSendJsonObjectHeader (int method, String url, JSONObject jsonRequest, 
           Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) { 
      super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, 
        errorListener); 

      // Log.i("Json Object",jsonRequest.toString()); 
     } 

     @Override 
     protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 

      try { 
       //Log.i("Response parse","Yes"); 
       String jsonString = new String(response.data, 
         HttpHeaderParser.parseCharset(response.headers)); 
       //Log.i("Json String",jsonString); 
       System.out.println("Json String is"+ jsonString); 

       //Log.i("Response Complete",response.toString()); 
       //Log.i("Response Data",response.data.toString()); 
       return Response.success(new JSONObject(jsonString), 
         HttpHeaderParser.parseCacheHeaders(response)); 
      } catch (UnsupportedEncodingException e) { 
       return Response.error(new ParseError(e)); 
      } catch (JSONException je) { 
       return Response.error(new ParseError(je)); 
      } 

     } 


     @Override 
     public String getBodyContentType() { 
      return "form-data"; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 

      //Log.i("Get Header","Enter"); 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      //headers.put("Content-Type", "application/json; charset=utf-8"); 
      //headers.put("Content-Type", "application/x-www-form-urlencoded"); 
      Log.i("TOken is ", Constants.getTokenDB()); 
      //System.out.println("Token Length is "+Constants.getTokenDB().length()); 
      headers.put("x-access-token", Constants.getTokenDB()); 

      //Log.i("Get Header","Exit"); 
      return headers; 

     } 

    } 

を使用して画像を送信しようとしています:

private void uploadImage(File file){ 
     //Showing the progress dialog 

     JSONObject jsonObject=new JSONObject(); 

     Uri path = Uri.fromFile(file); 

     System.out.println("File is "+path); 
     //String image=getStringImage(bt); 

     System.out.println("Achaha "+path); 
     try { 
      jsonObject.put("Profile",path); 
     }catch (JSONException js){ 
      js.printStackTrace(); 
     } 


     Response.Listener listener=new Response.Listener<JSONObject>() 
     { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 

        Boolean responseCond=response.getBoolean("success"); 

        System.out.println("Response "+responseCond); 

        String imagePath=response.getString("url"); 
        System.out.println("Image Path "+imagePath); 
       } 
       catch (JSONException k) 
       { 
        Log.i("On Response",k.getMessage()); 
        k.printStackTrace(); 
       } 

      } 

     }; 

     String image_url= Constants.url+"upload"; 

     imageSendJsonObjectHeader customRequest=new imageSendJsonObjectHeader(image_url,jsonObject, listener, Constants.errorListener); 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
     requestQueue.add(customRequest); 


    } 

試みた方法:

  1. File file=new File(file_path);

  2. File file=new File(file_path); Uri path = Uri.fromFile(file);

  3. は、文字列として画像を送信するビットマップ

  4. を送ります。

を入力し、profileフィールドに値を渡します。郵便配達を使用して

成功した方法:

enter image description here

+0

こんにちは@Ankurチェックアウトあなたに役立つかもしれないこのリンクhttp://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley?noredirect=1&lq = 1 –

+0

ちょっと@PiyushPatelクラスを作成中。 multipartEntityクラスオブジェクトの作成中にエラーが発生しました。私はそれが引数部分[]を渡す必要があると思う、HttpParams –

+0

マルチパートのための@Ankurちょっと@Ankur http://stackoverflow.com/questions/28470486/android-multipartentity-and- dependencies –

答えて

0

Base64文字列にそれを作る

public static String imgToBase64(Bitmap bitmap) { 
    ByteArrayOutputStream out = null; 
    try { 
     out = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out); 
     out.flush(); 
     out.close(); 
     byte[] imgBytes = out.toByteArray(); 
     return Base64.encodeToString(imgBytes, Base64.DEFAULT); 
    } catch (Exception e) { 
     return null; 
    } finally { 
     try { 
      out.flush(); 
      out.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

私はこれを試しました...「エラーが発生しました」「未定義のプロパティ '0'を読み取れません」 –

+0

アプリまたはサーバーですか?正しいビットマップを得ましたか? – Thinsky

+0

はいサーバーから正しいビットマップ..を取得しています。この応答が返されます。 –

0

@Ankur build.gradle useLibrary 'org.apache.http.legacy' でlagecyを使用

+0

bro。私が忘れていることは、あなたに伝えます。データはJSONにありますが、ここでは私が推測する文字列を扱っています –

関連する問題