2017-08-01 5 views
0

サーバーに更新されたファイルの量を更新して進行状況ダイアログに表示する進捗ダイアログを作成しようとしています。https://stackoverflow.com/a/33384551 )コード:更新のアンドロイドを使用してイメージとデシジョン(画像用)をアップロード中に進捗ダイアログを更新します

public class ProgressRequestBody extends RequestBody { 
    private File mFile; 
    private String mPath; 
    private UploadCallbacks mListener; 

private static final int DEFAULT_BUFFER_SIZE = 2048; 

public interface UploadCallbacks { 
    void onProgressUpdate(int percentage); 
    void onError(); 
    void onFinish(); 
} 

public ProgressRequestBody(final File file, final UploadCallbacks listener) { 
    mFile = file; 
    mListener = listener;    
} 

@Override 
public MediaType contentType() { 
    // i want to upload only images 
    return MediaType.parse("image/*"); 
} 

@Override 
public long contentLength() throws IOException { 
    return mFile.length(); 
} 

@Override 
public void writeTo(BufferedSink sink) throws IOException { 
    long fileLength = mFile.length(); 
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 
    FileInputStream in = new FileInputStream(mFile); 
    long uploaded = 0; 

    try { 
     int read; 
     Handler handler = new Handler(Looper.getMainLooper()); 
     while ((read = in.read(buffer)) != -1) { 

      // update progress on UI thread 
      handler.post(new ProgressUpdater(uploaded, fileLength)); 

      uploaded += read; 
      sink.write(buffer, 0, read); 
     } 
    } finally { 
     in.close(); 
    } 
} 

private class ProgressUpdater implements Runnable { 
    private long mUploaded; 
    private long mTotal; 
    public ProgressUpdater(long uploaded, long total) { 
     mUploaded = uploaded; 
     mTotal = total; 
    } 

    @Override 
    public void run() { 
     mListener.onProgressUpdate((int)(100 * mUploaded/mTotal));    
    } 
} 
} 

APIインタフェースにおいて:myActiviyで

@Multipart 
@POST("/upload")   
Call<JsonObject> uploadImage(@Part MultipartBody.Part file); 

私はファイルボディを渡すことができないなどのエラーを取得し

MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", 
      file.getName(), fileBody); 

に「ファイルボディ」オブジェクトを渡すしようとすると、

私が直面している問題があり、それは間違っている引数です。

私はこの投稿(https://stackoverflow.com/a/33384551)から参照を取得し、それと同じですが、私はerrosを取得しました。

答えて

0

不足しているファイル paramater。試してみてください

File file = new File(your image path); 
ProgressRequestBody fileBody = new ProgressRequestBody(file, this); 
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), fileBody); 
関連する問題