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);
}