diff --git a/acra-http/src/main/java/org/acra/http/MultipartHttpRequest.kt b/acra-http/src/main/java/org/acra/http/MultipartHttpRequest.kt index 00f5e56bb4..a8bfa12cd1 100644 --- a/acra-http/src/main/java/org/acra/http/MultipartHttpRequest.kt +++ b/acra-http/src/main/java/org/acra/http/MultipartHttpRequest.kt @@ -47,6 +47,7 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte .format(CONTENT_TYPE, contentType) .append(NEW_LINE) .append(content.first) + .append(NEW_LINE) for (uri in content.second) { try { val name = UriUtils.getFileNameFromUri(context, uri) @@ -56,6 +57,7 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte .append(NEW_LINE) .flush() UriUtils.copyFromUri(context, outputStream, uri) + writer.append(NEW_LINE) } catch (e: FileNotFoundException) { warn(e) { "Not sending attachment" } } @@ -67,8 +69,8 @@ class MultipartHttpRequest(config: CoreConfiguration, private val context: Conte private const val BOUNDARY = "%&ACRA_REPORT_DIVIDER&%" private const val BOUNDARY_FIX = "--" private const val NEW_LINE = "\r\n" - private const val SECTION_START = NEW_LINE + BOUNDARY_FIX + BOUNDARY + NEW_LINE - private const val MESSAGE_END = NEW_LINE + BOUNDARY_FIX + BOUNDARY + BOUNDARY_FIX + NEW_LINE + private const val SECTION_START = BOUNDARY_FIX + BOUNDARY + NEW_LINE + private const val MESSAGE_END = BOUNDARY_FIX + BOUNDARY + BOUNDARY_FIX + NEW_LINE private const val CONTENT_DISPOSITION = "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"$NEW_LINE" private const val CONTENT_TYPE = "Content-Type: %s$NEW_LINE" } diff --git a/acra-http/src/main/java/org/acra/util/UriUtils.kt b/acra-http/src/main/java/org/acra/util/UriUtils.kt index 4e488f77d6..7db82a093f 100644 --- a/acra-http/src/main/java/org/acra/util/UriUtils.kt +++ b/acra-http/src/main/java/org/acra/util/UriUtils.kt @@ -32,15 +32,25 @@ import java.io.OutputStream object UriUtils { @Throws(IOException::class) fun copyFromUri(context: Context, outputStream: OutputStream, uri: Uri) { - context.contentResolver.openInputStream(uri)?.copyTo(outputStream, bufferSize = ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) ?: throw FileNotFoundException( - "Could not open $uri") + context.contentResolver.openInputStream(uri)?.use { + it.copyTo(outputStream, bufferSize = ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) + } ?: throw FileNotFoundException("Could not open $uri") } @Throws(FileNotFoundException::class) fun getFileNameFromUri(context: Context, uri: Uri): String { - context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor -> - if (cursor.moveToFirst()) { - return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + val cursor = context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null) + if (cursor != null) { + @Suppress("ConvertTryFinallyToUseCall") // cursor is not Closeable until API 16 + try { + if (cursor.moveToFirst()) { + val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) + if (columnIndex != -1) { + return cursor.getString(columnIndex) + } + } + } finally { + cursor.close() } } throw FileNotFoundException("Could not resolve filename of $uri")