Save to TestStorage when saving Bitmap

PiperOrigin-RevId: 548733979
This commit is contained in:
Googler 2023-07-17 18:34:33 +01:00 committed by Ian Baker
parent 34768c2399
commit 7adc0af8a6

View file

@ -15,6 +15,7 @@
*/ */
package androidx.media3.test.utils; package androidx.media3.test.utils;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState; import static androidx.media3.common.util.Assertions.checkState;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext; import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@ -42,6 +43,8 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
@ -342,6 +345,19 @@ public class BitmapPixelTestUtil {
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Could not write Bitmap to file path: " + file.getAbsolutePath(), 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);
}
} }
/** /**