Repository: expo/expo
File: packages/expo-sharing/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt (Kotlin)

Code before:
        contentType = ContentType.fromMimeType(specificMimeType)
        contentSize = fileSize
        contentMimeType = specificMimeType
        originalName = fileName
      }
    }

    private fun getFileName(resolver: ContentResolver, uri: Uri): String? {
      var name: String? = null
      val cursor = resolver.query(uri, null, null, null, null)
      cursor?.use {
        if (it.moveToFirst()) {
          val nameIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME)
          if (nameIndex != -1) {
            name = it.getString(nameIndex)
          }
        }
      }

      return name ?: uri.lastPathSegment
    }

    private fun getFileSize(resolver: ContentResolver, uri: Uri): Long? {
      val cursor = resolver.query(uri, null, null, null, null)
      cursor?.use {
        if (it.moveToFirst()) {
          val sizeIndex = it.getColumnIndex(OpenableColumns.SIZE)
          if (sizeIndex != -1) {
            return it.getLong(sizeIndex)
          }
        }
      }
      return null
    }

    private fun resolveUrlContext(urlString: String): ResolvedSharePayload {
      val payload = ResolvedSharePayload().apply {
        value = urlString
        shareType = ShareType.Url
        mimeType = "text/plain"
      }

      try {
        val url = URL(urlString)
        val connection = url.openConnection() as HttpURLConnection

        // Use GET instead of HEAD. Even though we want only the head, this works better with
        // redirects (e.g., https://picsum.photos/200/300), where requesting the HEAD will not redirect to the
        // final photo.
        connection.requestMethod = "GET"


Review comment (suggestion):
```suggestion
      val cursor = resolver.query(uri, null, null, null, null)
        ?: return null
```

Code after:
        contentType = ContentType.fromMimeType(specificMimeType)
        contentSize = fileSize
        contentMimeType = specificMimeType
        originalName = fileName
      }
    }

    private fun getFileName(resolver: ContentResolver, uri: Uri): String? {
      val cursor = resolver.query(uri, null, null, null, null)
      val name = cursor?.use { cursor ->
        val nameIndex = cursor
          .takeIf { it.moveToFirst() }
          ?.getColumnIndex(OpenableColumns.DISPLAY_NAME)

        if (nameIndex != null && nameIndex != -1) {
          cursor.getString(nameIndex)
        } else {
          null
        }
      }

      return name ?: uri.lastPathSegment
    }

    private fun getFileSize(resolver: ContentResolver, uri: Uri): Long? {
      val cursor = resolver.query(uri, null, null, null, null)
        ?: return null

      return cursor.use { cursor ->
        val sizeIndex = cursor
          .takeIf { it.moveToFirst() }
          ?.getColumnIndex(OpenableColumns.SIZE)

        if (sizeIndex != null && sizeIndex != -1) {
          cursor.getLong(sizeIndex)
        } else {
          null
        }
      }
    }

    private fun resolveUrlContext(urlString: String): ResolvedSharePayload {
      val payload = ResolvedSharePayload().apply {
        value = urlString
        shareType = ShareType.Url
        mimeType = "text/plain"
      }

      try {
        val url = URL(urlString)
        val connection = url.openConnection() as HttpURLConnection