Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun uploadNilai(path: Uri?) {
- try {
- Log.e(TAG, "onActivityResult: ${context?.let { path?.let { it1 -> fileFromContentUri(it, it1) } }}" )
- val mFile = RequestBody.create(MediaType.parse("application/vnd.ms-excel"), context?.let { path?.let { it1 -> fileFromContentUri(it, it1) } })
- val excelUpload = MultipartBody.Part.createFormData("template", context?.let { FileUtils.getFileName(it, path) }, mFile)
- val idmatakuliah = RequestBody.create(MediaType.parse("text/plain"), idmatakuliah)
- val idstafdosen = RequestBody.create(MediaType.parse("text/plain"), runBlocking { userPrefrences.idStafDosenFlow.first() })
- viewModel.uploadNilaiModel(idmatakuliah, idstafdosen, excelUpload)
- val progressDialogManager = context?.let { it -> ProgressDialogManager(it).loader("Sedang mengirim data") }
- viewModel.uploadNilaiResponse.observe(viewLifecycleOwner, {
- when (it) {
- is Resource.Loading -> progressDialogManager?.show()
- is Resource.Success -> {
- val responseModel: ResponseModel = it.value
- val decrypt = EncDec().toDerypt(responseModel.streams)
- Log.e("TAG", "upload : $decrypt")
- progressDialogManager?.dismiss()
- }
- is Resource.Failure -> {
- progressDialogManager?.dismiss()
- // handleApiError(it)
- Log.e(TAG, "uploadNilai: $it")
- }
- }
- })
- } catch (e: Exception){
- e.printStackTrace()
- }
- }
- fun fileFromContentUri(context: Context, contentUri: Uri): File {
- // Preparing Temp file name
- val fileExtension = getFileExtension(context, contentUri)
- val fileName = "temp_file"+ if (fileExtension != null) ".$fileExtension" else ""
- // Creating Temp file
- val tempFile = File(context.cacheDir, fileName)
- tempFile.createNewFile()
- try {
- val oStream = FileOutputStream(tempFile)
- val inputStream = context.contentResolver.openInputStream(contentUri)
- inputStream?.let {
- copy(inputStream, oStream)
- }
- oStream.flush()
- } catch (e: Exception) {
- e.printStackTrace()
- }
- return tempFile
- }
- private fun getFileExtension(context: Context, uri: Uri): String? {
- val fileType: String? = context.contentResolver.getType(uri)
- return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType)
- }
- @Throws(IOException::class)
- private fun copy(source: InputStream, target: OutputStream) {
- val buf = ByteArray(8192)
- var length: Int
- while (source.read(buf).also { length = it } > 0) {
- target.write(buf, 0, length)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement