Функция, которая выполняет отправку: < /p>
Код: Выделить всё
fun sendVideoToTelegram(
chatId: Long,
videoFile: File,
caption: String = "") {
if (!videoFile.exists() || !videoFile.isFile) {
println("Error: Video file does not exist or is not a file: " +
"${videoFile.absolutePath}")
// Handle error appropriately (e.g., notify user, log)
return
}
CoroutineScope(Dispatchers.IO).launch {
val result = ibot.sendVideo(
chatId = ChatId.fromId(chatId),
video = TelegramFile.ByFile(videoFile),
caption = caption
)
result.fold(
{ response ->
// Successfully sent
val message = response?.result
Log.i(TAG, "Video sent successfully! Message ID:" +
" ${message?.messageId}")
// Optionally, you can delete the local file after successful upload
// videoFile.delete()
},
{ error ->
// Handle error
Log.i(TAG, "Error sending video: ${error.errorBody?.string()
?: error.exception?.message}")
// Log the error, notify the user, retry, etc.
}
)
}
}
Ошибка отправка видео:
/storage/emult/0/movies/motionRecords/motionRec-2025-09-17-17-06-49-989.mp4:br/> open: eAcces (plmces unied)
< /code
Запрашивать разрешения в композитной функции: < /p>
var hasCameraPermission by remember {
mutableStateOf(
ContextCompat.checkSelfPermission(
context, Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED)
}
// Add audio permission if you enable audio recording
var hasStoragePermission by remember {
mutableStateOf(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
true
}// No explicit permission needed for MediaStore above Q for app's own contributions
else
{
(ContextCompat.checkSelfPermission(
context, Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED)
}
)
}
var hasStoragePermissionR by remember {
mutableStateOf(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
true
}// No explicit permission needed for MediaStore above Q for app's own contributions
else
{
(ContextCompat.checkSelfPermission(
context, Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED)
}
)
}
// Add Audio permission if enabling audio
// var hasAudioPermission by remember { mutableStateOf(...) }
val permissionsToRequest = mutableListOf()
if (!hasCameraPermission) {
permissionsToRequest.add(Manifest.permission.CAMERA)
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (!hasStoragePermission) {
permissionsToRequest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
if (!hasStoragePermissionR) {
permissionsToRequest.add(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
// if (!hasAudioPermission) permissionsToRequest.add(Manifest.permission.RECORD_AUDIO)
val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
hasCameraPermission =
permissions[Manifest.permission.CAMERA] ?:
hasCameraPermission
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
hasStoragePermission =
permissions[Manifest.permission.WRITE_EXTERNAL_STORAGE] ?:
hasStoragePermission
hasStoragePermissionR =
permissions[Manifest.permission.READ_EXTERNAL_STORAGE] ?:
hasStoragePermissionR
}
// hasAudioPermission = permissions[Manifest.permission.RECORD_AUDIO] ?: hasAudioPermission
if (hasCameraPermission && hasStoragePermission && hasStoragePermissionR
/* && hasAudioPermission */) {
// Permissions granted, manager will be created or
// re-triggered if key changes
} else {
Log.e("MotionRecordScreen",
"Required permissions not granted.")
// Handle permission denial (e.g., show a message to the user)
}
}
< /code>
Файл отправляется в той же функции: < /p>
LaunchedEffect(lastRecordedVideoFile) {
// check that messages are being sent
videoSender.sendMessageToTelegram(
myChatID,
lastRecordedVideoFile?.path ?: "file is null"
)
lastRecordedVideoFile?.let { videoFile ->
Log.d(TAG, "File Exist: ${videoFile.exists()}")
if (videoFile.exists()) {
Log.d(TAG, "New video recorded:" +
" ${videoFile.absolutePath}. Attempting to send...")
val targetChatId = myChatID
videoSender.sendVideoToTelegram(
chatId = targetChatId,
videoFile = videoFile,
caption = "Motion detected and recorded!"
)
// Optionally, reset lastRecordedVideoFile in your manager or state
// to prevent resending if the Composable recomposes for other reasons.
}
}
}
[/code]
Я начал писать на Android совсем недавно, я все еще учусь, и многие вещи все еще неясны.
Подробнее здесь: https://stackoverflow.com/questions/797 ... e-internet
Мобильная версия