fix: 增加图片预览

This commit is contained in:
xiaoyan 2023-01-06 13:39:07 +08:00
parent 8e5f216b42
commit f114a2da79
5 changed files with 52 additions and 24 deletions

View File

@ -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
}

View File

@ -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<String, String>)
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,6 +284,7 @@ class ObtainMessageViewModel: ViewModel() {
* 根据网络地址获取本地的缓存文件路径
* */
fun getLocalFileFromNetUrl(url: String, attachmentType: AttachmentType):File {
if (url.startsWith("http")) {
val folder = when(attachmentType) {
AttachmentType.PIC-> SystemConstant.CameraFolder
else -> SystemConstant.SoundFolder
@ -290,5 +295,8 @@ class ObtainMessageViewModel: ViewModel() {
url.substringAfterLast("/")
}
return File(folder, name)
} else {
return File(url)
}
}
}

View File

@ -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)

View File

@ -122,6 +122,7 @@
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_photo_name"
android:textColor="@android:color/holo_blue_dark"
android:padding="@dimen/default_widget_padding"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"></com.google.android.material.textview.MaterialTextView>
@ -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=""></com.google.android.material.textview.MaterialTextView>
<Space

View File

@ -1,5 +1,6 @@
pluginManagement {
repositories {
mavenCentral()
maven { url "https://maven.aliyun.com/repository/google" }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/public' }
@ -8,12 +9,12 @@ pluginManagement {
jcenter()
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url "https://maven.aliyun.com/repository/google" }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/public' }
@ -21,7 +22,6 @@ dependencyResolutionManagement {
maven { url 'https://jitpack.io' }
jcenter()
google()
mavenCentral()
}
}
rootProject.name = "NavinfoVolvo"