Я хочу, когда закончите запись экрана, загрузите его на сервер.
Я пишу ниже коды, но показывает ошибку.
Мои коды:
Код: Выделить всё
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityCompressUploadBinding.inflate(layoutInflater)
setContentView(binding.root)
//File uri
lifecycleScope.launch {
videoData.getVideoPath.collect { storedPath ->
if (storedPath.isNullOrEmpty()) {
fileUri = if (SDK_INT >= VERSION_CODES.Q) FILE_URI else FILE_PATH?.convertFileToUri()
testId = TEST_ID
testTitle = TEST_TITLE
//videoData.saveVideoPath(fileUri.toString())
} else {
fileUri = Uri.parse(storedPath)
videoData.getVideoInfo.collect { info ->
if (info.isNullOrEmpty().not()) {
val infoSplit = info?.split(SPLITTER)!!
testId = infoSplit[0].toInt()
testTitle = "${infoSplit[1]} $testId"
}
}
}
}
}
lifecycleScope.launch {
delay(300)
val inputStream: InputStream = contentResolver.openInputStream(fileUri!!)!!
val file = File.createTempFile("video", ".mp4")
inputStream.use { input ->
val outputStream = FileOutputStream(file)
input.copyTo(outputStream)
}
fileUri?.let { uri ->
val parcelFile = contentResolver.openFileDescriptor(uri, "r", null)
val inputStream = FileInputStream(parcelFile!!.fileDescriptor)
val file = File(cacheDir, contentResolver.getFileName(uri) ?: "temp_video.mp4")
val outputStream = FileOutputStream(file)
inputStream.copyTo(outputStream)
outputStream.close()
inputStream.close()
if (file.exists() && file.length() > 0) {
val fileName = URLEncoder.encode(file.name, "UTF-8")
val progressRequest = ProgressRequestBody(file, "multipart/form-data") { progress ->
runOnUiThread {
binding.apply {
uploadProgress.progress = progress
uploadPercentageTxt.text = "$progress%"
}
}
}
val filePart = MultipartBody.Part.createFormData("file", fileName, progressRequest)
bodyUpload.file = filePart
videoData.saveVideoPath(uri.toString())
viewModel.callPostUploadVideoNew(testId, bodyUpload)
Log.e("FilePathLog", "File: ${file.path} --- Name : $fileName")
} else {
Log.e("FileError", "File was not created or is empty")
}
}
}
}
Код: Выделить всё
java.io.FileNotFoundException: /data/user/0/com.myapp/cache (Is a directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:308)
at java.io.FileOutputStream.(FileOutputStream.java:238)
at java.io.FileOutputStream.(FileOutputStream.java:180)
at com.testady.ui.compress_upload.CompressUploadActivity$onCreate$2.invokeSuspend(CompressUploadActivity.kt:167)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:235)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497)
at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:595)
at kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$$inlined$Runnable$1.run(Runnable.kt:19)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException
Я поискал это в Google и Stackoverflow, но ничего не нашел!
Подробнее здесь: https://stackoverflow.com/questions/793 ... n-androidi
Мобильная версия