In this challenge, you are tasked with completing a simple Android application that selects a file from the device and uploads it to Cloudinary. You are provided with a partially implemented MainActivity and must complete both the Cloudinary initialization and the file upload logic.
Your goal is to implement the missing logic to ensure the application correctly configures the Cloudinary MediaManager, captures the selected file URI, and successfully triggers the upload request.
You are given an Android project with a MainActivity.kt file containing a shell of the application logic.
You need to perform the following operations:
- Cloudinary Setup: Inside
setupCloudinary(), initialize theMediaManagerby providing your Cloudinary configuration parameters (i.e.,cloud_name,api_key, andapi_secret). - Handle File Selection: Inside the
filePickerLaunchercallback, properly assign the selectedurito thefileUriclass variable. - Trigger File Upload: Implement the
uploadFile()function using the CloudinaryMediaManager.get().upload(uri)API. Ensure you pass your actual upload preset instead of the default placeholder, and include the proper callbacks to handle upload states.
To successfully pass the assignment requirements, your implementation must satisfy the following checks:
- The Cloudinary upload preset must not be left as
"stub_preset", empty, or blank. - The application must gracefully handle valid and null URIs using the provided companion object validation checks (
isValidUri).
Run the unit tests:
./gradlew testThe unit tests are designed to fail initially. Your task is to modify MainActivity.kt until all tests in CloudinarySetupTest.kt and InputValidationTest.kt pass.
Cloudinary Initialization
Ensure you initialize the MediaManager safely in setupCloudinary(). If the manager is already initialized, it throws an exception, which is why a try-catch block is provided. Replace the comments with actual initialization logic.
File Selection state
When the filePickerLauncher successfully returns a file, assign the result to the globally declared fileUri variable so it is accessible within uploadFile().
Upload Implementation Details
You must call MediaManager.get().upload(uri). Be sure to chain the required methods such as .unsigned("your_actual_upload_preset") (or .option("upload_preset", "your_actual_preset")), .callback(...), and finally call .dispatch().
- Replace the
"stub_preset"placeholder text with the actual upload preset created in your Cloudinary console. - In
setupCloudinary(), use aHashMapof configuration values containing the keys"cloud_name","api_key", and"api_secret"and pass it toMediaManager.init(). - Use Android UI capabilities to update the interface based on the
onStart,onProgress,onSuccess, andonErrormethods of the Cloudinary upload callback! - Remember: The code is intentionally left partially unimplemented. Your structural problem-solving and understanding of third-party SDK integration are key!