diff --git a/maestro-android/src/androidTest/java/dev/mobile/maestro/MaestroDriverService.kt b/maestro-android/src/androidTest/java/dev/mobile/maestro/MaestroDriverService.kt
index 083238f866..7a31662e22 100644
--- a/maestro-android/src/androidTest/java/dev/mobile/maestro/MaestroDriverService.kt
+++ b/maestro-android/src/androidTest/java/dev/mobile/maestro/MaestroDriverService.kt
@@ -246,6 +246,8 @@ class Service(
override fun onNext(value: MaestroAndroid.AddMediaRequest) {
if (outputStream == null) {
outputStream = MediaStorage.getOutputStream(
+ InstrumentationRegistry.getInstrumentation()
+ .context,
value.mediaName,
value.mediaExt
)
diff --git a/maestro-android/src/androidTest/java/dev/mobile/maestro/Media.kt b/maestro-android/src/androidTest/java/dev/mobile/maestro/Media.kt
index ade1567a9d..9b50f8b8fb 100644
--- a/maestro-android/src/androidTest/java/dev/mobile/maestro/Media.kt
+++ b/maestro-android/src/androidTest/java/dev/mobile/maestro/Media.kt
@@ -1,13 +1,21 @@
package dev.mobile.maestro
+import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.ContentValues
+import android.content.Context
+import android.content.pm.PackageManager
import android.provider.MediaStore
+import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import java.io.OutputStream
object MediaStorage {
- fun getOutputStream(mediaName: String, mediaExt: String): OutputStream? {
+ fun getOutputStream(context: Context, mediaName: String, mediaExt: String): OutputStream? {
+ if (context.checkSelfPermission(WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
+ Log.e("MediaStorage", "Permission $WRITE_EXTERNAL_STORAGE not granted")
+ return null
+ }
val uri = when (mediaExt) {
Service.FileType.JPG.ext,
Service.FileType.PNG.ext,
diff --git a/maestro-android/src/main/AndroidManifest.xml b/maestro-android/src/main/AndroidManifest.xml
index 21b09433e5..8f16d86fc1 100644
--- a/maestro-android/src/main/AndroidManifest.xml
+++ b/maestro-android/src/main/AndroidManifest.xml
@@ -11,6 +11,9 @@
tools:ignore="ProtectedPermissions" />
+
diff --git a/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt b/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
index cd23477a80..312cb64888 100644
--- a/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
+++ b/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
@@ -710,6 +710,7 @@ class AndroidDriver(
override fun addMedia(mediaFiles: List) {
LOGGER.info("[Start] Adding media files")
+ dadb.shell("pm grant dev.mobile.maestro android.permission.WRITE_EXTERNAL_STORAGE")
mediaFiles.forEach { addMediaToDevice(it) }
LOGGER.info("[Done] Adding media files")
}
diff --git a/maestro-client/src/main/resources/maestro-app.apk b/maestro-client/src/main/resources/maestro-app.apk
index 4af2d623e9..a6eb400417 100644
Binary files a/maestro-client/src/main/resources/maestro-app.apk and b/maestro-client/src/main/resources/maestro-app.apk differ
diff --git a/maestro-client/src/main/resources/maestro-server.apk b/maestro-client/src/main/resources/maestro-server.apk
index bdbc33015c..d8cf86ec7d 100644
Binary files a/maestro-client/src/main/resources/maestro-server.apk and b/maestro-client/src/main/resources/maestro-server.apk differ