0

I have a few methods in my activity and would like to move them to a different class as I will use it again later. This allows me to upload an image to my server, it works very well in my activity, but I would like to move it.

The problem, I am a beginner in java and I don't know how to, can someone tell me how to do this?

Thank you for your help !

below, the code I want to move

@Override
protected void onResume() {
    super.onResume();
    uploadReceiver.register(this);
}

@Override
protected void onPause() {
    super.onPause();
    uploadReceiver.unregister(this);
}

private void pickFile() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select image"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
        uploadMultipart(data.getData().toString());
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
            pictureProfileInformation.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

private String getPath(Uri uri){
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);

    document_id = document_id.substring(document_id.lastIndexOf(":")+1);
    cursor.close();

    cursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID+" = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}

private void uploadMultipart(String filePath) {
    try {
        String uploadId = UUID.randomUUID().toString();
        uploadReceiver.setUploadID(uploadId);
        String name = tilUsernameInformation.getEditText().getText().toString().trim();

        String path = getPath(Uri.parse(filePath));
        String extension = path.substring(path.indexOf("."));
        String sessionPhoto = (name+extension).toLowerCase();

        String id_user =sessionManager.getUserData().get(sessionManager.ID_USER);

        new MultipartUploadRequest(this, uploadId, URL_UPLOAD)
                .setMethod("POST")
                .addFileToUpload(filePath, "image")
                .addParameter("name", name)
                .addParameter("id_user", id_user)
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(0)
                .startUpload();

        sessionManager.changeData("PHOTO", sessionPhoto);

    } catch (Exception exc) {
        Log.e("Upload start error", exc.getMessage(), exc);
    }
}

@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
    showMessage("Progress: " +  uploadInfo.getProgressPercent());
}

@Override
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {
    showMessage("Error uploading. Server response code: " +  serverResponse.getHttpCode() + ", body: " + serverResponse.getBodyAsString());
}

@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
    showMessage("Completed. Server response code: " +  serverResponse.getHttpCode() + ", body: " + serverResponse.getBodyAsString());
}

@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
    showMessage("Upload cancelled");
}

private void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    Log.i("Message", message);
}
SeeZix
  • 3
  • 5
  • 3
    You can't.Those are methods from the interface you implemented . So it is mandatory to have those in the same class of implementation – Umeshwaran Jan 29 '21 at 20:26
  • 1
    You can have more detailed explanation in https://www.tutorialspoint.com/must-we-implement-all-the-methods-in-a-class-that-implements-an-interface-in-java#:~:text=Yes%2C%20it%20is%20mandatory%20to,declared%20as%20an%20abstract%20class.&text=Declare%20the%20class%20as%20an,you%20can%20create%20any%20objects. and https://stackoverflow.com/questions/11437097/not-implementing-all-of-the-methods-of-interface-is-it-possible – Umeshwaran Jan 29 '21 at 20:28

1 Answers1

0

You can use a simple hierarchy to manage it

In a parent class (maybe also abstract) you can define these general methods that you want to reuse in your activities

public abstract class ParentActivity {

    [some code]

    @Override
    protected void onResume() {
        super.onResume();
        uploadReceiver.register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        uploadReceiver.unregister(this);
    }

    [other Override methods]

}

and then yours activities can extend it

public class MyActivity extends ParentActivity {
    [code]
}