Android share intent: logging + temp file cleanup (#10762)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+28
-6
@@ -6,6 +6,7 @@ import android.media.MediaMetadataRetriever
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
|
import android.util.Log
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import expo.modules.kotlin.modules.Module
|
import expo.modules.kotlin.modules.Module
|
||||||
import expo.modules.kotlin.modules.ModuleDefinition
|
import expo.modules.kotlin.modules.ModuleDefinition
|
||||||
@@ -13,6 +14,8 @@ import java.io.File
|
|||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.net.URLEncoder
|
import java.net.URLEncoder
|
||||||
|
|
||||||
|
private const val TAG = "ExpoReceiveAndroidIntents"
|
||||||
|
|
||||||
enum class AttachmentType {
|
enum class AttachmentType {
|
||||||
IMAGE,
|
IMAGE,
|
||||||
VIDEO,
|
VIDEO,
|
||||||
@@ -161,14 +164,25 @@ class ExpoReceiveAndroidIntentsModule : Module() {
|
|||||||
// app, since this runs synchronously on the module init path.
|
// app, since this runs synchronously on the module init path.
|
||||||
try {
|
try {
|
||||||
FileOutputStream(file).use { out ->
|
FileOutputStream(file).use { out ->
|
||||||
val input = appContext.currentActivity?.contentResolver?.openInputStream(uri) ?: return
|
val input =
|
||||||
|
appContext.currentActivity?.contentResolver?.openInputStream(uri)
|
||||||
|
?: run {
|
||||||
|
file.delete()
|
||||||
|
return
|
||||||
|
}
|
||||||
input.use { it.copyTo(out) }
|
input.use { it.copyTo(out) }
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Failed to copy shared video to cache", e)
|
||||||
|
file.delete()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val info = getVideoInfo(uri) ?: return
|
val info =
|
||||||
|
getVideoInfo(uri) ?: run {
|
||||||
|
file.delete()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val encodedText = text?.let { URLEncoder.encode(it, "UTF-8") }
|
val encodedText = text?.let { URLEncoder.encode(it, "UTF-8") }
|
||||||
|
|
||||||
@@ -188,15 +202,22 @@ class ExpoReceiveAndroidIntentsModule : Module() {
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// The URI may be unreadable (revoked permission, deleted file, or a
|
// The URI may be unreadable (revoked permission, deleted file, or a
|
||||||
// provider that rejects the read). Skip this image rather than crash.
|
// provider that rejects the read). Skip this image rather than crash.
|
||||||
|
Log.w(TAG, "Failed to read shared image", e)
|
||||||
return null
|
return null
|
||||||
} ?: return null
|
} ?: return null
|
||||||
// We have to save this so that we can access it later when uploading the image.
|
// We have to save this so that we can access it later when uploading the image.
|
||||||
// createTempFile will automatically place a unique string between "img" and "temp.jpeg"
|
// createTempFile will automatically place a unique string between "img" and "temp.jpeg"
|
||||||
val file = createFile("jpeg")
|
val file = createFile("jpeg")
|
||||||
val out = FileOutputStream(file)
|
try {
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
|
FileOutputStream(file).use { out ->
|
||||||
out.flush()
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
|
||||||
out.close()
|
out.flush()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Failed to write shared image to cache", e)
|
||||||
|
file.delete()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
return mapOf(
|
return mapOf(
|
||||||
"width" to bitmap.width,
|
"width" to bitmap.width,
|
||||||
@@ -215,6 +236,7 @@ class ExpoReceiveAndroidIntentsModule : Module() {
|
|||||||
height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toIntOrNull()
|
height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toIntOrNull()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// The URI may be unreadable or not a valid media source. Skip rather than crash.
|
// The URI may be unreadable or not a valid media source. Skip rather than crash.
|
||||||
|
Log.w(TAG, "Failed to read shared video metadata", e)
|
||||||
return null
|
return null
|
||||||
} finally {
|
} finally {
|
||||||
retriever.release()
|
retriever.release()
|
||||||
|
|||||||
Reference in New Issue
Block a user