Mada za sehemu hiiWorking with data and multimediaMada 2
- Using local data storage
- Showing images and videos
Showing Images and Videos in Mobile Apps
Mobile apps become more engaging when they display visual content like photos and videos. In this topic, you will learn how to add images and videos to your Android applications using specialized libraries and components.
Images and videos help users understand information quickly. A photo of a product in an e-commerce app tells a buyer more than text alone. A video tutorial in an education app can demonstrate a skill better than written instructions. As a mobile developer, you must know how to embed these media types into your apps.
Android apps can load images from different sources:
- Local storage – images saved in the app's drawable folder
- Device gallery – photos taken by the user
- Internet – images downloaded from URLs
Loading images from the internet requires special handling. You need to download the image, process it, and display it without blocking your app's interface.
Two popular Android libraries make loading images easier: Glide and Picasso. Both handle downloading, caching, and displaying images with minimal code.
Glide Library
Glide is widely used because it handles image loading efficiently. It downloads images in the background, caches them for fast loading later, and automatically resizes images to fit your app's display area.
To use Glide in your app, add this dependency to your build.gradle file:
implementation 'com.github.bumptech.glide:glide:4.15.1'
Then load an image into an ImageView like this:
ImageView imageView = findViewById(R.id.product_image);
String imageUrl = "https://example.com/photo.jpg";
Glide.with(this)
.load(imageUrl)
.into(imageView);
Picasso Library
Picasso offers similar features with slightly different syntax. It also handles downloading and caching automatically.
ImageView imageView = findViewById(R.id.product_image);
String imageUrl = "https://example.com/photo.jpg";
Picasso.get()
.load(imageUrl)
.into(imageView);
Both libraries prevent your app from freezing while downloading images. They also cache images, so repeat visits to the same image load instantly.
Videos can be played using built-in Android components or external libraries.
Using Intent to Open Video Player
The simplest method uses an Intent to open the device's default video player:
String videoUrl = "https://example.com/video.mp4";
Uri videoUri = Uri.parse(videoUrl);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(videoUri, "video/mp4");
startActivity(intent);
This opens the video in whichever player the user has installed on their device.
Using ExoPlayer Library
For more control, developers use ExoPlayer. This library lets you play videos directly inside your app with custom controls.
Add the dependency:
implementation 'com.google.android.exoplayer:exoplayer:2.19.1'
Create a SimpleExoPlayer and connect it to a PlayerView:
SimpleExoPlayer player = new SimpleExoPlayer.Builder(this).build();
PlayerView playerView = findViewById(R.id.player_view);
playerView.setPlayer(player);
player.prepare();
MediaSource mediaSource = new ProgressiveMediaSource.Factory(
new DefaultDataSourceFactory(this, "MyApp"))
.createMediaSource(Uri.parse("https://example.com/video.mp4"));
player.setPlayWhenReady(true);
player.prepare(mediaSource);
ExoPlayer supports streaming videos from the internet, playing local files, and handling different video formats.
Imagine you are creating an app for a Dar es Salaam market vendor to display their products. Here is how you might display product images:
- Add the ImageView in your layout XML:
<ImageView
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />
- Load the image using Glide in your Java activity:
public class ProductActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
ImageView imageView = findViewById(R.id.product_image);
String imageUrl = "https://mbeBeach.com/fruits/mangoes.jpg";
Glide.with(this)
.load(imageUrl)
.placeholder(R.drawable.loading) // image shown while loading
.error(R.drawable.error) // image shown if error occurs
.into(imageView);
}
}
This code displays mangoes from a market website. While the image downloads, a loading placeholder appears. If the download fails, an error image shows instead.
- Images and videos make mobile apps more useful and engaging
- Glide and Picasso simplify loading images from the internet
- Use Intents for simple video playback in external players
- ExoPlayer provides full control for playing videos inside your app
- Always handle loading states (placeholders) and errors in your code
- Test your multimedia features on real devices with different screen sizes
- Always load images asynchronously to keep your app responsive
- Cache images to save bandwidth and improve speed
- Handle network errors gracefully with fallback images
- Test video streaming on slower network connections common in Tanzania
A small supermarket in Arusha can use a mobile app to show customers product images and promotional videos. The owner loads product photos using Glide so customers can browse items like rice, cooking oil, and soap on their phones. The app also streams video advertisements about weekly specials, helping the shop attract more buyers without printing flyers — this saves money and reaches more customers quickly through their mobile phones.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Display images from local storage, URLs, or APIs.
- Integrate video playback features (e.g., play, pause, seek).
Tathmini ya matokeo
Mobile app that effectively displays images and plays videos, with smooth transitions and user-friendly controls.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to
- integrate multimedia in mobile apps
Principles: The student should explain the principles of:
- responsiveness, caching, and smooth playback.
Theories: The student should explain:
- Types of media supported by mobile apps
- the role of compression formats and streaming protocols.
Circumstantial knowledge:
Detailed knowledge about:
- challenges like slow loading, unsupported formats, or resourceintensive multimedia content.
Swali
Which Android library is specifically designed to load, cache, and display images from the internet?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado