From 7adc0af8a67686acfea9c2493419f48d7fe37c18 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 17 Jul 2023 18:34:33 +0100 Subject: [PATCH] Save to TestStorage when saving Bitmap PiperOrigin-RevId: 548733979 --- .../media3/test/utils/BitmapPixelTestUtil.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libraries/test_utils/src/main/java/androidx/media3/test/utils/BitmapPixelTestUtil.java b/libraries/test_utils/src/main/java/androidx/media3/test/utils/BitmapPixelTestUtil.java index 8746b51fa8..e3326b2daf 100644 --- a/libraries/test_utils/src/main/java/androidx/media3/test/utils/BitmapPixelTestUtil.java +++ b/libraries/test_utils/src/main/java/androidx/media3/test/utils/BitmapPixelTestUtil.java @@ -15,6 +15,7 @@ */ package androidx.media3.test.utils; +import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkState; import static androidx.test.core.app.ApplicationProvider.getApplicationContext; import static com.google.common.truth.Truth.assertThat; @@ -42,6 +43,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.Arrays; @@ -342,6 +345,19 @@ public class BitmapPixelTestUtil { } catch (IOException e) { Log.e(TAG, "Could not write Bitmap to file path: " + file.getAbsolutePath(), e); } + + try { + // Use reflection here as this is an experimental API that may not work for all users + Class testStorageClass = Class.forName("androidx.test.services.storage.TestStorage"); + Method method = testStorageClass.getMethod("openOutputFile", String.class); + Object testStorage = testStorageClass.getDeclaredConstructor().newInstance(); + OutputStream outputStream = (OutputStream) method.invoke(testStorage, fileName); + bitmap.compress(Bitmap.CompressFormat.PNG, /* quality= */ 100, checkNotNull(outputStream)); + } catch (ClassNotFoundException e) { + // Do nothing + } catch (Exception e) { + Log.i(TAG, "Could not write Bitmap to test storage: " + fileName, e); + } } /**