Android — How to Provide Photos to the Gallery for Running UI Automated Tests

Wongsatorn Chanprasopchai
2 min readAug 30, 2024

--

When working on UI automation tests for Android applications, it is often necessary to manipulate the device’s media, such as adding photos to the gallery. This allows you to simulate real user behavior and verify that your app handles media correctly. In this guide, I’ll show you how to programmatically add photos to the gallery on an Android emulator.

Step 1: Create a Bitmap Image

First, we need to create a bitmap image that will serve as the photo in the gallery. For simplicity, let’s generate a 100x100 pixel image filled with a solid blue color:

private fun provideImageToGallery() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888).apply {
eraseColor(Color.BLUE)
}
val filename = getRandomString(10)
addTestImageToMediaStore(context, bitmap, filename)
}

In this code, we create a 100x100 bitmap and fill it with a blue color using the eraseColor(Color.BLUE) method. The getRandomString(10) function is used to generate a random filename for the image.

Step 2: Save the Bitmap to the Media Store

Next, we need to save the bitmap to the device’s media store so that it appears in the gallery. This is done with the addTestImageToMediaStore function:

private fun addTestImageToMediaStore(context: Context, bitmap: Bitmap, filename: String): Uri? {
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, "$filename.jpg")
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
}
val uri = context.contentResolver.insert(MediaStore.Files.getContentUri("external"), contentValues)
uri?.let {
context.contentResolver.openOutputStream(it).use { outputStream ->
if (outputStream != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
}
}
}
return uri
}

Here’s how this works:

  1. ContentValues: We create a ContentValues object to define the metadata for our image, including its display name and MIME type.
  2. Insert into Media Store: We insert the image metadata into the media store by calling context.contentResolver.insert(). This returns a Uri where the image can be stored.
  3. Write Image Data: Using the Uri, we open an output stream and write the bitmap data into the media store as a JPEG image.

Step 3: Generate a Random Filename

To avoid conflicts with existing files, it’s a good practice to generate a random filename. Here’s a simple function that does just that:

private fun getRandomString(length: Int): String {
val charset = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..length)
.map { charset.random() }
.joinToString("")
}

This function generates a random string of the specified length using letters and numbers, ensuring that each file has a unique name.

Conclusion

With this approach, you can easily add photos to the gallery of an Android emulator during UI automated tests. This is particularly useful for testing features that interact with media files, like photo viewers or editors. By programmatically creating and saving images, you can ensure your tests are consistent and repeatable.

--

--