diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt index 519bf347488..d93e1903885 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/camera/ImageStoreManager.kt @@ -5,26 +5,21 @@ * LICENSE file in the root directory of this source tree. */ -// TODO T185922447 migrate from deprecated AsyncTask -@file:Suppress("DEPRECATION") - package com.facebook.react.modules.camera import android.net.Uri -import android.os.AsyncTask import android.util.Base64 import android.util.Base64OutputStream import com.facebook.fbreact.specs.NativeImageStoreAndroidSpec import com.facebook.react.bridge.Callback -import com.facebook.react.bridge.GuardedAsyncTask import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.bridge.ReactContext import com.facebook.react.module.annotations.ReactModule import java.io.ByteArrayOutputStream import java.io.Closeable import java.io.FileNotFoundException import java.io.IOException import java.io.InputStream +import java.util.concurrent.Executors @ReactModule(name = NativeImageStoreAndroidSpec.NAME) public class ImageStoreManager(reactContext: ReactApplicationContext) : @@ -38,21 +33,12 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) : * @param error callback to be invoked on error (e.g. file not found, not readable etc.) */ override public fun getBase64ForTag(uri: String, success: Callback, error: Callback) { - GetBase64Task(getReactApplicationContext(), uri, success, error) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) - } - - private inner class GetBase64Task( - reactContext: ReactContext, - private val uri: String, - private val success: Callback, - private val error: Callback - ) : GuardedAsyncTask(reactContext) { - protected override fun doInBackgroundGuarded(vararg params: Void?) { + val executor = Executors.newSingleThreadExecutor() + executor.execute { try { val contentResolver = getReactApplicationContext().getContentResolver() - val uri = Uri.parse(uri) - val inputStream = contentResolver.openInputStream(uri) as InputStream + val parsedUri = Uri.parse(uri) + val inputStream = contentResolver.openInputStream(parsedUri) as InputStream try { success.invoke(convertInputStreamToBase64OutputStream(inputStream)) } catch (e: IOException) { @@ -66,22 +52,6 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) : } } - @Throws(IOException::class) - internal fun convertInputStreamToBase64OutputStream(inputStream: InputStream): String { - val baos = ByteArrayOutputStream() - val b64os = Base64OutputStream(baos, Base64.NO_WRAP) - val buffer = ByteArray(BUFFER_SIZE) - var bytesRead: Int - try { - while (inputStream.read(buffer).also { bytesRead = it } > -1) { - b64os.write(buffer, 0, bytesRead) - } - } finally { - closeQuietly(b64os) // this also closes baos and flushes the final content to it - } - return baos.toString() - } - public companion object { public const val NAME: String = NativeImageStoreAndroidSpec.NAME @@ -94,5 +64,21 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) : // shhh } } + + @Throws(IOException::class) + internal fun convertInputStreamToBase64OutputStream(inputStream: InputStream): String { + val baos = ByteArrayOutputStream() + val b64os = Base64OutputStream(baos, Base64.NO_WRAP) + val buffer = ByteArray(BUFFER_SIZE) + var bytesRead: Int + try { + while (inputStream.read(buffer).also { bytesRead = it } > -1) { + b64os.write(buffer, 0, bytesRead) + } + } finally { + closeQuietly(b64os) // this also closes baos and flushes the final content to it + } + return baos.toString() + } } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/camera/ImageStoreManagerTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/camera/ImageStoreManagerTest.kt index 8381d2470e4..9811ff79300 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/camera/ImageStoreManagerTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/camera/ImageStoreManagerTest.kt @@ -71,8 +71,7 @@ class ImageStoreManagerTest { } private fun invokeConversion(inputStream: InputStream): String { - return ImageStoreManager(reactApplicationContext) - .convertInputStreamToBase64OutputStream(inputStream) + return ImageStoreManager.convertInputStreamToBase64OutputStream(inputStream) } private fun generateRandomByteString(length: Int): ByteArray {