android-upload-service
android-upload-service copied to clipboard
duplicate uploadID
Hi ,
Describe the bug I am using the upload manager in my fragment but I get You have tried to perform startUpload() using the same uploadID of an " + "already running task. You're trying to use the same ID for multiple uploads. error message when debugging the app
To Reproduce
public class App extends Application {
UploadServiceConfig.initialize(this, App.CHANNEL, BuildConfig.DEBUG);
UploadServiceConfig.setHttpStack(new OkHttpStack(getOkHttpClient()));
UploadServiceConfig.setRetryPolicy(new RetryPolicyConfig(1, 10, 2, 3));
}
public class photoFragment extends Fragment {
protected UploadNotificationConfig getNotificationConfig(final String uploadId, @StringRes int title) {
PendingIntent clickIntent = PendingIntent.getActivity(
getActivity(), 1, new Intent(getContext(), photoFragment.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
final boolean autoClear = false;
final boolean clearOnAction = true;
final boolean ringToneEnabled = true;
final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1);
final UploadNotificationAction cancelAction = new UploadNotificationAction(
R.drawable.ic_cancelled,
getString(R.string.cancel_upload),
ContextExtensionsKt.getCancelUploadIntent(getContext(), uploadId)
);
final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1);
progressActions.add(cancelAction);
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.uploading),
R.drawable.ic_upload,
Color.BLUE,
null,
clickIntent,
progressActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_success),
R.drawable.ic_upload_success,
Color.GREEN,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_error),
R.drawable.ic_upload_error,
Color.RED,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_cancelled),
R.drawable.ic_cancelled,
Color.YELLOW,
null,
clickIntent,
noActions,
clearOnAction
);
return new UploadNotificationConfig(App.CHANNEL, ringToneEnabled, progress, success, error, cancelled);
}
try{
MultipartUploadRequest request = new MultipartUploadRequest(context2, serverUrl)
.addFileToUpload(String.valueOf(filePath),"file")
.setMethod("POST")
.setNotificationConfig((context, uploadId)->getNotificationConfig(uploadId, R.string.app_name))
.setMaxRetries(2);
request.startUpload();
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
}
}
OS and Lib versions (please complete the following information): minSdk = 26 targetSdk = 34 versionCode = 1 versionName = "1"
I need to app to upload successfully but it is not!
The code you posted seems correct at first glance, but since you're getting that error, it means that two or more requests have been made with the same uploadId, that's an internal protection of the lib, so check the rest of your code to be sure to not have a fixed uploadId somewhere and to not have duplicate requests being made (for example a quick double tap by the user).
Also, if all your uploads needs the same notification config, better to use the notification config factory method (check the configuration wiki).
Il gio 25 apr 2024, 20:42 keyvan @.***> ha scritto:
Hi ,
Describe the bug I am using the upload manager in my fragment but I get You have tried to perform startUpload() using the same uploadID of an " + "already running task. You're trying to use the same ID for multiple uploads. error message when debugging the app
To Reproduce
public class App extends Application { UploadServiceConfig.initialize(this, App.CHANNEL, BuildConfig.DEBUG); UploadServiceConfig.setHttpStack(new OkHttpStack(getOkHttpClient())); UploadServiceConfig.setRetryPolicy(new RetryPolicyConfig(1, 10, 2, 3)); }
` public class photoFragment extends Fragment { protected UploadNotificationConfig getNotificationConfig(final String uploadId, @StringRes int title) { PendingIntent clickIntent = PendingIntent.getActivity( getActivity(), 1, new Intent(getContext(), photoFragment.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
final boolean autoClear = false; final boolean clearOnAction = true; final boolean ringToneEnabled = true; final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1); final UploadNotificationAction cancelAction = new UploadNotificationAction( R.drawable.ic_cancelled, getString(R.string.cancel_upload), ContextExtensionsKt.getCancelUploadIntent(getContext(), uploadId) ); final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1); progressActions.add(cancelAction); UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig( getString(title), getString(R.string.uploading), R.drawable.ic_upload, Color.BLUE, null, clickIntent, progressActions, clearOnAction, autoClear ); UploadNotificationStatusConfig success = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_success), R.drawable.ic_upload_success, Color.GREEN, null, clickIntent, noActions, clearOnAction, autoClear ); UploadNotificationStatusConfig error = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_error), R.drawable.ic_upload_error, Color.RED, null, clickIntent, noActions, clearOnAction, autoClear ); UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_cancelled), R.drawable.ic_cancelled, Color.YELLOW, null, clickIntent, noActions, clearOnAction ); return new UploadNotificationConfig(App.CHANNEL, ringToneEnabled, progress, success, error, cancelled);}
try{ MultipartUploadRequest request = new MultipartUploadRequest(context2, serverUrl) .addFileToUpload(String.valueOf(filePath),"file") .setMethod("POST") .setNotificationConfig((context, uploadId)->getNotificationConfig(uploadId, R.string.app_name)) .setMaxRetries(2); request.startUpload(); } catch (Exception exc) { Log.e(TAG, exc.getMessage(), exc); } } `
Expected behavior A clear and concise description of what you expected to happen.
OS and Lib versions (please complete the following information): minSdk = 26 targetSdk = 34 versionCode = 1 versionName = "1"
— Reply to this email directly, view it on GitHub https://github.com/gotev/android-upload-service/issues/661, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEADXLYPF3A656U4IFZAXJLY7FFAFAVCNFSM6AAAAABGZNPY5SVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI3DIMRWGQ4DKMA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
The code you posted seems correct at first glance, but since you're getting that error, it means that two or more requests have been made with the same uploadId, that's an internal protection of the lib, so check the rest of your code to be sure to not have a fixed uploadId somewhere and to not have duplicate requests being made (for example a quick double tap by the user). Also, if all your uploads needs the same notification config, better to use the notification config factory method (check the configuration wiki). Il gio 25 apr 2024, 20:42 keyvan @.> ha scritto: … Hi , Describe the bug I am using the upload manager in my fragment but I get You have tried to perform startUpload() using the same uploadID of an " + "already running task. You're trying to use the same ID for multiple uploads. error message when debugging the app To Reproduce public class App extends Application { UploadServiceConfig.initialize(this, App.CHANNEL, BuildConfig.DEBUG); UploadServiceConfig.setHttpStack(new OkHttpStack(getOkHttpClient())); UploadServiceConfig.setRetryPolicy(new RetryPolicyConfig(1, 10, 2, 3)); }
public class photoFragment extends Fragment { protected UploadNotificationConfig getNotificationConfig(final String uploadId, @StringRes int title) { PendingIntent clickIntent = PendingIntent.getActivity( getActivity(), 1, new Intent(getContext(), photoFragment.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); final boolean autoClear = false; final boolean clearOnAction = true; final boolean ringToneEnabled = true; final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1); final UploadNotificationAction cancelAction = new UploadNotificationAction( R.drawable.ic_cancelled, getString(R.string.cancel_upload), ContextExtensionsKt.getCancelUploadIntent(getContext(), uploadId) ); final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1); progressActions.add(cancelAction); UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig( getString(title), getString(R.string.uploading), R.drawable.ic_upload, Color.BLUE, null, clickIntent, progressActions, clearOnAction, autoClear ); UploadNotificationStatusConfig success = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_success), R.drawable.ic_upload_success, Color.GREEN, null, clickIntent, noActions, clearOnAction, autoClear ); UploadNotificationStatusConfig error = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_error), R.drawable.ic_upload_error, Color.RED, null, clickIntent, noActions, clearOnAction, autoClear ); UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig( getString(title), getString(R.string.upload_cancelled), R.drawable.ic_cancelled, Color.YELLOW, null, clickIntent, noActions, clearOnAction ); return new UploadNotificationConfig(App.CHANNEL, ringToneEnabled, progress, success, error, cancelled); } try{ MultipartUploadRequest request = new MultipartUploadRequest(context2, serverUrl) .addFileToUpload(String.valueOf(filePath),"file") .setMethod("POST") .setNotificationConfig((context, uploadId)->getNotificationConfig(uploadId, R.string.app_name)) .setMaxRetries(2); request.startUpload(); } catch (Exception exc) { Log.e(TAG, exc.getMessage(), exc); } }Expected behavior A clear and concise description of what you expected to happen. OS and Lib versions (please complete the following information): minSdk = 26 targetSdk = 34 versionCode = 1 versionName = "1" — Reply to this email directly, view it on GitHub <#661>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEADXLYPF3A656U4IFZAXJLY7FFAFAVCNFSM6AAAAABGZNPY5SVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI3DIMRWGQ4DKMA . You are receiving this because you are subscribed to this thread.Message ID: @.>
The breakpoint only hits once I will post the whole fragment though
here is the whole fragment:
public class photoFragment extends Fragment {
private static final int CAMERA_REQUEST = 1888;
private static final int MY_CAMERA_PERMISSION_CODE = 100;
private ImageButton imageBtn;
private ImageButton imageBtn2;
LinearLayout imageArea;
FlexboxLayout imageArea2;
android.widget.ProgressBar progressBar;
android.widget.ProgressBar progressBar2;
TextView referenceNumberTextView;
String refNumber;
Integer buttonClicked;
Integer imageCount = 0;
private static final String TAG = "AndroidUploadService";
public photoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo, container, false);
imageBtn = rootView.findViewById(R.id.imageButton1);
imageBtn2 = rootView.findViewById(R.id.imageButton2);
imageArea = rootView.findViewById(R.id.imageArea);
imageArea2 = rootView.findViewById(R.id.imageArea2);
progressBar = rootView.findViewById(R.id.progressBar);
progressBar2 = rootView.findViewById(R.id.progressBar2);
referenceNumberTextView = rootView.findViewById(R.id.referenceNumberTextView);
refNumber = getArguments().getString("refNumber");
if (refNumber != null && refNumber != ""){
referenceNumberTextView.setText(refNumber);
}
imageBtn.setOnClickListener(v -> {
buttonClicked = 1;
openGallery();
});
imageBtn2.setOnClickListener(v -> {
buttonClicked = 2;
if (imageCount >= 7){
Utilities.setupDialog(getContext(),R.string.errorFormTitle,R.string.errorUploadLimit);
}else{
openGallery();
}
});
return rootView ;
}
public void openGallery(){
if (ContextCompat.checkSelfPermission(getContext(),Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
Log.d("camera","request permission");
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Log.d("camera","ACTION_PICK");
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getContext(), getResources().getString(R.string.camer_access_granted), Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(getContext(), getResources().getString(R.string.camer_access_denied), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != Activity.RESULT_CANCELED) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri selectedImage = data.getData();
// if (selectedImage == null){return;}
uploadCoverPhoto(getContext(), selectedImage);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
ImageView placeholderImg = new ImageView(getContext());
placeholderImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (90 * scale + 0.5f);
placeholderImg.setImageBitmap(bitmap);
if (buttonClicked == 1) {
imageBtn.setImageBitmap(bitmap);
} else {
imageArea2.addView(placeholderImg);
placeholderImg.getLayoutParams().width = dpAsPixels;
placeholderImg.getLayoutParams().height = dpAsPixels;
placeholderImg.requestLayout();
}
}
}
}
}
public void uploadCoverPhoto(final Context context2,Uri filePath) {
try{
String serverUrl = "";
if (buttonClicked == 1){
serverUrl = UrlRepository.getUploadPhoto() + "/" + refNumber;
}else{
serverUrl = UrlRepository.getUploadPhoto() + "/extra/" + refNumber;
}
MultipartUploadRequest request = new MultipartUploadRequest(context2, serverUrl)
.addFileToUpload(String.valueOf(filePath),"file")
.setMethod("POST")
.setNotificationConfig((context, uploadId)->getNotificationConfig(uploadId, R.string.app_name))
.setMaxRetries(2);
request.startUpload();
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
}
}
protected UploadNotificationConfig getNotificationConfig(final String uploadId, @StringRes int title) {
PendingIntent clickIntent = PendingIntent.getActivity(
getActivity(), 1, new Intent(getContext(), photoFragment.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
final boolean autoClear = false;
final boolean clearOnAction = true;
final boolean ringToneEnabled = true;
final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1);
final UploadNotificationAction cancelAction = new UploadNotificationAction(
R.drawable.ic_cancelled,
getString(R.string.cancel_upload),
ContextExtensionsKt.getCancelUploadIntent(getContext(), uploadId)
);
final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1);
progressActions.add(cancelAction);
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.uploading),
R.drawable.ic_upload,
Color.BLUE,
null,
clickIntent,
progressActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_success),
R.drawable.ic_upload_success,
Color.GREEN,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_error),
R.drawable.ic_upload_error,
Color.RED,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(
getString(title),
getString(R.string.upload_cancelled),
R.drawable.ic_cancelled,
Color.YELLOW,
null,
clickIntent,
noActions,
clearOnAction
);
return new UploadNotificationConfig(App.CHANNEL, ringToneEnabled, progress, success, error, cancelled);
}
}
The problem may be because in your case you invoke the camera and the onActivityResult is handled in the fragment instead of in the activity containing the fragment.
Android best practices suggest an approach like this
public class YourFragment extends Fragment {
private static final int REQUEST_CODE = 123;
// Your fragment code...
public void startForResult() {
Intent intent = new Intent(getActivity(), YourActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
// Delegate result handling to the parent activity
((YourActivity) getActivity()).handleActivityResult(resultCode, data);
}
}
}
Parent activity
public class YourActivity extends AppCompatActivity {
// Your activity code...
public void handleActivityResult(int resultCode, Intent data) {
// Handle the result here
if (resultCode == Activity.RESULT_OK) {
// Handle successful result
} else {
// Handle unsuccessful result
}
}
}
Or better, delegate the whole StartActivityForResult and result handling to the parent activity, and start upload from there. Fragments have often inconsistent lifecycles unfortunately.
As a further debug utility, you can log UploadService.taskList which contains all the currently active tasks.
The problem may be because in your case you invoke the camera and the onActivityResult is handled in the fragment instead of in the activity containing the fragment.
Android best practices suggest an approach like this
public class YourFragment extends Fragment { private static final int REQUEST_CODE = 123; // Your fragment code... public void startForResult() { Intent intent = new Intent(getActivity(), YourActivity.class); startActivityForResult(intent, REQUEST_CODE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { // Delegate result handling to the parent activity ((YourActivity) getActivity()).handleActivityResult(resultCode, data); } } }Parent activity
public class YourActivity extends AppCompatActivity { // Your activity code... public void handleActivityResult(int resultCode, Intent data) { // Handle the result here if (resultCode == Activity.RESULT_OK) { // Handle successful result } else { // Handle unsuccessful result } } }Or better, delegate the whole StartActivityForResult and result handling to the parent activity, and start upload from there. Fragments have often inconsistent lifecycles unfortunately.
As a further debug utility, you can log UploadService.taskList which contains all the currently active tasks.
I found something interesting even if put the 'upload part ONLY' in another activity it gives the same error:duplicateUpliad id! I have no idea! maybe it has something to do with the App class!
Check how you are performing the init of the library
To my surprise the code below works ofcourse I upgraded some of the okhttp one version up
implementation ("net.gotev:uploadservice:4.7.0")
implementation ("com.nononsenseapps:filepicker:4.1.0")
implementation ("net.gotev:uploadservice-okhttp:4.9.2")
implementation ("com.jakewharton:butterknife:10.2.3")
implementation ("com.squareup.okhttp3:logging-interceptor:4.12.0")
String uploadId2 = UUID.randomUUID().toString();
PendingIntent clickIntent = PendingIntent.getActivity(
getActivity(), 1, new Intent(getContext(), photoFragment.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
final boolean autoClear = false;
final boolean clearOnAction = true;
final boolean ringToneEnabled = true;
final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1);
final UploadNotificationAction cancelAction = new UploadNotificationAction(
R.drawable.ic_cancelled,
getString(R.string.cancel_upload),
ContextExtensionsKt.getCancelUploadIntent(getContext(), uploadId2)
);
final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1);
progressActions.add(cancelAction);
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.uploading),
R.drawable.ic_upload,
Color.BLUE,
null,
clickIntent,
progressActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.upload_success),
R.drawable.ic_upload_success,
Color.GREEN,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.upload_error),
R.drawable.ic_upload_error,
Color.RED,
null,
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.upload_cancelled),
R.drawable.ic_cancelled,
Color.YELLOW,
null,
clickIntent,
noActions,
clearOnAction
);
final MultipartUploadRequest request = new MultipartUploadRequest(context, serverUrl);
request.addFileToUpload(String.valueOf(filePath),"file");
request.setMethod("POST");
request.setUploadID(uploadId2);
request.setNotificationConfig((context2, uploadId)->new UploadNotificationConfig(App.CHANNEL, ringToneEnabled, progress, success, error, cancelled));
request.setMaxRetries(2);
request.startUpload();
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
}
I see quite old libraries used there I wouldn't recommend to be used in 2024 android development (like ButterKnife). Latest upload service is 4.9.2. I recommend you to use that instead. What you're experiencing is surely because of some underlying lifecycle problems which are beyond the scope of the lib, given the evidence I've seen so far.