diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt index ff01973..3a84345 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt @@ -2,6 +2,7 @@ package com.navinfo.volvo.ui.fragments.message import android.content.DialogInterface import android.graphics.Paint +import android.net.Uri import android.os.Bundle import android.text.TextUtils import android.view.LayoutInflater @@ -21,9 +22,11 @@ import androidx.lifecycle.ViewModelProvider import androidx.navigation.Navigation import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.crazylegend.audiopicker.pickers.SingleAudioPicker import com.easytools.tools.DateUtils import com.easytools.tools.DeviceUtils import com.easytools.tools.DisplayUtils +import com.easytools.tools.FileIOUtils import com.easytools.tools.FileUtils import com.easytools.tools.ResourceUtils import com.easytools.tools.ToastUtils @@ -45,11 +48,14 @@ import com.navinfo.volvo.util.PhotoLoader import com.navinfo.volvo.utils.EasyMediaFile import com.navinfo.volvo.utils.SystemConstant import com.nhaarman.supertooltips.ToolTip +import com.robertlevonyan.components.picker.* import indi.liyi.viewer.Utils import indi.liyi.viewer.ViewData import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream import java.util.* @@ -145,7 +151,7 @@ class ObtainMessageFragment: Fragment() { binding.layerAudioResult.visibility = if (hasAudio) VISIBLE else GONE binding.layerGetAudio.visibility = if (hasAudio) GONE else VISIBLE - binding.llAudioPlay.visibility = if (hasAudio) VISIBLE else GONE +// binding.llAudioPlay.visibility = if (hasAudio) VISIBLE else GONE } ) lifecycle.addObserver(recorderLifecycleObserver) @@ -240,6 +246,17 @@ class ObtainMessageFragment: Fragment() { // 用户选择录音文件 binding.btnSelectSound.setOnClickListener { photoHelper.setCrop(false).selectAudio(activity!!) +// SingleAudioPicker.showPicker(context!!) { +// val audioFile = File(it.contentUri.path) +// ToastUtils.showToast(audioFile.absolutePath) +// if (!audioFile.parentFile.parentFile.absolutePath.equals(SystemConstant.SoundFolder)) { +// val copyResult = FileIOUtils.writeFileFromIS(File(SystemConstant.SoundFolder, audioFile.name), FileInputStream(audioFile)) +// XLog.e("拷贝结果:"+copyResult) +// obtainMessageViewModel.updateMessageAudio(File(SystemConstant.SoundFolder, audioFile.name).absolutePath) +// } else { +// obtainMessageViewModel.updateMessageAudio(audioFile.absolutePath) +// } +// } } binding.btnStartRecord.setOnTouchListener { view, motionEvent -> @@ -315,7 +332,8 @@ class ObtainMessageFragment: Fragment() { } // 如果当前文件不在camera缓存文件夹下,则移动该文件 if (!file!!.parentFile.absolutePath.equals(SystemConstant.CameraFolder)) { - FileUtils.renameFile(file.absolutePath, File(SystemConstant.CameraFolder, fileName).absolutePath) + val copyResult = FileIOUtils.writeFileFromIS(File(SystemConstant.CameraFolder, fileName), FileInputStream(file)) + XLog.e("拷贝结果:"+copyResult) // 跳转回原Fragment,展示拍摄的照片 ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java).updateMessagePic(File(SystemConstant.CameraFolder, fileName).absolutePath) } else { @@ -331,7 +349,8 @@ class ObtainMessageFragment: Fragment() { } else if (fileName.endsWith(".mp3")||fileName.endsWith(".wav")||fileName.endsWith(".amr")||fileName.endsWith(".m4a")) { ToastUtils.showToast(it.absolutePath) if (!it.parentFile.parentFile.absolutePath.equals(SystemConstant.SoundFolder)) { - FileUtils.renameFile(it.absolutePath, File(SystemConstant.SoundFolder, fileName).absolutePath) + val copyResult = FileIOUtils.writeFileFromIS(File(SystemConstant.SoundFolder, fileName), FileInputStream(it)) + XLog.e("拷贝结果:"+copyResult) obtainMessageViewModel.updateMessageAudio(File(SystemConstant.SoundFolder, fileName).absolutePath) } else { obtainMessageViewModel.updateMessageAudio(it.absolutePath) @@ -344,7 +363,8 @@ class ObtainMessageFragment: Fragment() { ToastUtils.showToast(it.message) } - binding.voicePlayerView.setOnClickListener { + binding.tvAudioName.setOnClickListener { + binding.llAudioPlay.visibility = if (binding.llAudioPlay.visibility == VISIBLE) GONE else VISIBLE // 判断当前播放的文件是否在缓存文件夹内,如果不在首先下载该文件 val fileUrl = obtainMessageViewModel.getMessageLiveData().value!!.mediaUrl!! val localFile = obtainMessageViewModel.getLocalFileFromNetUrl(fileUrl, AttachmentType.AUDIO) @@ -511,10 +531,6 @@ class ObtainMessageFragment: Fragment() { .showIndex(true) // 是否显示图片索引,默认为true .watch(0) // 开启浏览 - } - // 点击音频名称 - binding.tvAudioName.setOnClickListener { - } } @@ -563,4 +579,6 @@ class ObtainMessageFragment: Fragment() { super.onDestroy() lifecycle.removeObserver(recorderLifecycleObserver) } + + companion object } \ No newline at end of file diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt index 8d101db..3f4eb0c 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt @@ -1,6 +1,7 @@ package com.navinfo.volvo.ui.fragments.message import androidx.lifecycle.* +import com.easytools.tools.FileIOUtils import com.easytools.tools.FileUtils import com.easytools.tools.ToastUtils import com.elvishew.xlog.XLog @@ -19,6 +20,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody import java.io.File +import java.io.FileInputStream import java.util.* @@ -140,13 +142,15 @@ class ObtainMessageViewModel: ViewModel() { if (destFile.exists()) { FileUtils.deleteFile(destFile) } - FileUtils.renameFile(attachmentFile.absolutePath, destFile.absolutePath) + val copyResult = FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) + XLog.e("拷贝结果:"+copyResult) } else { val destFile = File(SystemConstant.SoundFolder, newFileName) if (destFile.exists()) { FileUtils.deleteFile(destFile) } - FileUtils.renameFile(attachmentFile.absolutePath, destFile.absolutePath) + val copyResult = FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) + XLog.e("拷贝结果:"+copyResult) } if (fileKey!=null) { downloadAttachment(fileKey, attachmentType) @@ -234,6 +238,7 @@ class ObtainMessageViewModel: ViewModel() { // 获取上传后的结果 val netId = result.data message?.id = netId!!.toLong() + ToastUtils.showToast("保存成功") // TODO 尝试更新本地数据 } else { @@ -262,9 +267,8 @@ class ObtainMessageViewModel: ViewModel() { val result = NavinfoVolvoCall.getApi().updateCardByApp(updateData as Map) XLog.d("updateCardByApp:${result.code}") if (result.code == 200) { // 请求成功 - // 获取上传后的结果 - val netId = result.data - message?.id = netId!!.toLong() + // 数据更新成功 + ToastUtils.showToast("更新成功") // 尝试保存数据到本地 } else { ToastUtils.showToast(result.msg) @@ -280,15 +284,19 @@ class ObtainMessageViewModel: ViewModel() { * 根据网络地址获取本地的缓存文件路径 * */ fun getLocalFileFromNetUrl(url: String, attachmentType: AttachmentType):File { - val folder = when(attachmentType) { - AttachmentType.PIC-> SystemConstant.CameraFolder - else -> SystemConstant.SoundFolder - } - var name = if (url.contains("?")) { - url.substring(url.lastIndexOf("/")+1, url.indexOf("?")) + if (url.startsWith("http")) { + val folder = when(attachmentType) { + AttachmentType.PIC-> SystemConstant.CameraFolder + else -> SystemConstant.SoundFolder + } + var name = if (url.contains("?")) { + url.substring(url.lastIndexOf("/")+1, url.indexOf("?")) + } else { + url.substringAfterLast("/") + } + return File(folder, name) } else { - url.substringAfterLast("/") + return File(url) } - return File(folder, name) } } \ No newline at end of file diff --git a/app/src/main/java/com/navinfo/volvo/utils/EasyMediaFile.kt b/app/src/main/java/com/navinfo/volvo/utils/EasyMediaFile.kt index eebd1cf..b159971 100644 --- a/app/src/main/java/com/navinfo/volvo/utils/EasyMediaFile.kt +++ b/app/src/main/java/com/navinfo/volvo/utils/EasyMediaFile.kt @@ -103,7 +103,7 @@ class EasyMediaFile { fun selectAudio(activity: Activity) { isCrop = false val intent = Intent(Intent.ACTION_PICK, null).apply { - setDataAndType(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "audio/*") + setDataAndType(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "*/*") } if (Looper.myLooper() == Looper.getMainLooper()) { selectFileInternal(intent, activity, 1) diff --git a/app/src/main/res/layout/fragment_obtain_message.xml b/app/src/main/res/layout/fragment_obtain_message.xml index 77f28cc..e90f9a4 100644 --- a/app/src/main/res/layout/fragment_obtain_message.xml +++ b/app/src/main/res/layout/fragment_obtain_message.xml @@ -122,6 +122,7 @@ @@ -208,6 +209,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_dark" + android:padding="@dimen/default_widget_padding" android:layout_weight="1" android:text="">