2016-06-11 4 views
0

Retrofitライブラリを使用してサーバー上の画像をアップロードする方法はありますか?このような詳細なシナリオ私はPHP Web APIを使用してサーバー上でユーザープロファイルのイメージと他のユーザーの詳細を送信したいというサインインモジュールを持っています。知っている人は誰でもよく知っているでしょう。また、アップロードするには最高の高速メソッドを知っているRetrofitライブラリを使用してマルチパートを使用してサーバーに画像をアップロードするには

+0

は、このチュートリアル... http://findnerd.com/list/view/Image-Upload-using-Retrofit/19788/ –

+0

これを試してみてくださいhttp://stackoverflow.com/questions/36491096/retrofitを試してみてください-multipart-request-required-multipartfile-parameter-file-not-pre/36514662#36514662 – BNK

答えて

0

私はサーバー上の資産イメージをアップロードするために作った私はまた、SDカードの画像をアップロードすることができますアセット画像のパスをSDカードのパスに置き換えます。他の人々もこの質問の仕事の解決のために私の努力を願っています。私の場合は正常に動作し、Retrofitを使用してプロジェクトの目的のイメージをアップロードします。 コードは次のとおりです。

 file= CopyReadAssets(); 
    okhttp3.RequestBody requestBody=okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"), file); 
    // MultipartBody.Part is used to send also the actual file name 
    body =MultipartBody.Part.createFormData("uploadfile", file.getName(), requestBody); 
    btnUpload.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      HashMap<String,String> map=new HashMap<String, String>(); 
      map.put("user_id","331"); 
      map.put("uploadfile","uploadfile"); 
      map.put("version_key_android","1.0"); 
      Call<String> call= null; 
      try { 
       String requestString=getWebservicejsObjRequestforvolley("createUser",map); 
       Log.d("requestString==>",requestString+""); 
       progressBar.setVisibility(View.VISIBLE); 
       call = apiService.upload(requestString,body); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      call.enqueue(new Callback<String>() { 
       @Override 
       public void onResponse(Call<String> call, Response<String> response) { 
        if(progressBar!=null){ 
         progressBar.setVisibility(View.GONE); 
        } 
        if(response.isSuccessful()){ 
         Log.d("status==>",response.body()+""); 
        } 
       } 
       @Override 
       public void onFailure(Call<String> call, Throwable t) { 
        if(progressBar!=null){ 
         progressBar.setVisibility(View.GONE); 
        } 
       } 
      }); 
     } 
    }); 
} 
private File CopyReadAssets() { 
    AssetManager assetManager = getAssets(); 
    InputStream in = null; 
    OutputStream out = null; 
    File file = new File(getFilesDir(), "temp.jpg"); 
    try { 
     in = assetManager.open("screen_1.jpg"); 
     out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 
     copyFile(in, out); 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     Log.e("tag", e.getMessage()); 
    } 
    Log.d("file==>",file.toString()+""); 
    return file; 
} 
private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

public String getWebservicejsObjRequestforvolley(String mMethodname, HashMap<String, String> mMap) throws JSONException { 
    String retStr; 
    //String strParams = null; 
    // map.put("device_id", Prefs.getDeviceTokenPref(BaseActivity.this)); 
    JSONObject json = new JSONObject(); 
    if (mMap.size() > 0) { 
     for (Map.Entry<String, String> e : mMap.entrySet()) { 
      String key = e.getKey(); 
      String value = e.getValue(); 
      json.put(key, value); 
     } 
    } 
    JSONObject fJson = new JSONObject(); 
    fJson.put("method_name", mMethodname); 
    fJson.put("body", json); 
    retStr = fJson.toString(); 
    return retStr; 
} 
関連する問題