The MainActivity Class
Explore the MainActivity class in an Android app that uses TensorFlow Lite for image classification. Understand how it initializes UI components, loads images, handles user interactions, and integrates the image classifier model to classify selected images and display results.
We'll cover the following...
The MainActivity class is the main activity of our app that extends the AppCompatActivity class. It overrides the onCreate() method to set up the UI, initializes the UI components, and handles user interactions.
Fields of the MainActivity
The fields of the MainActivity include:
A constant field to define the request code for loading an image.
A field (
Button) to load images from the storage.A field (
ImageView) to view the image on the UI.A field (
Bitmap) to represent the selected image.A field (of the type
ImageClassifier) to perform image classification using a TF Lite model.A field (
TextView) to display the classification result.A field (an instance of
ActivityResultLauncher)handling the result of picking an image.
The following code defines these fields:
Lines 18–20: These define a companion object (
REQUEST_CODE_LOAD_IMAGE) with a constant property to represent the request code for loading an image. ...