public void upload_file_multipart() {
String url = " http://server.printme.uz/v1/order/make-order";
try {
String uploadId = UUID.randomUUID().toString();
uploadReceiver.setDelegate((SingleUploadBroadcastReceiver.Delegate) this);
uploadReceiver.setUploadID(uploadId);
//Creating a multi part request
MultipartUploadRequest request = new MultipartUploadRequest(this, uploadId, UPLOAD_URL);
request.addFileToUpload(images.getPath(), "url");
request.setMaxRetries(2);
request.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Add this code your Activity class
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_id) {
upload_file_multipart();
dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Uploading photo, please wait.");
dialog.setMax(100);
dialog.setCancelable(true);
dialog.show();
}
}
implements SingleUploadBroadcastReceiver.Delegate to your Activity and declare like bellow
public class YourActivity extends AppCompatActivity implements SingleUploadBroadcastReceiver.Delegate {
private final SingleUploadBroadcastReceiver uploadReceiver =
new SingleUploadBroadcastReceiver();
ProgressDialog dialog;
@Override
protected void onResume() {
super.onResume();
uploadReceiver.register(this);
}
@Override
protected void onPause() {
super.onPause();
uploadReceiver.unregister(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
//your codes
}
@Override
public void onProgress(int progress) {
Log.d("PROGRESS", "progress = " + progress);
dialog.setProgress(progress);
}
@Override
public void onProgress(long uploadedBytes, long totalBytes) {
}
@Override
public void onError(Exception exception) {
}
@Override
public void onCompleted(int serverResponseCode, byte[] serverResponseBody) {
dialog.dismiss();
}
@Override
public void onCancelled() {
}
Here is SingleUploadBroadcastReceiver class
import net.gotev.uploadservice.UploadServiceBroadcastReceiver;
public class SingleUploadBroadcastReceiver extends UploadServiceBroadcastReceiver {
public interface Delegate {
void onProgress(int progress);
void onProgress(long uploadedBytes, long totalBytes);
void onError(Exception exception);
void onCompleted(int serverResponseCode, byte[] serverResponseBody);
void onCancelled();
}
private String mUploadID;
private Delegate mDelegate;
public void setUploadID(String uploadID) {
mUploadID = uploadID;
}
public void setDelegate(Delegate delegate) {
mDelegate = delegate;
}
@Override
public void onProgress(String uploadId, int progress) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onProgress(progress);
}
}
@Override
public void onProgress(String uploadId, long uploadedBytes, long totalBytes) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onProgress(uploadedBytes, totalBytes);
}
}
@Override
public void onError(String uploadId, Exception exception) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onError(exception);
}
}
@Override
public void onCompleted(String uploadId, int serverResponseCode, byte[] serverResponseBody) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onCompleted(serverResponseCode, serverResponseBody);
}
}
@Override
public void onCancelled(String uploadId) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onCancelled();
}
}
}