desc:导出数据到U盘

main
xiaowusky 2 years ago
parent 2880db4d61
commit 70127cd262

@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.FileUtils
import android.provider.Settings
import com.common.commonlib.db.DBUtils
import com.common.commonlib.db.entity.Gas
@ -21,6 +22,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
@ -105,7 +107,12 @@ object TestUtils {
// })
}
fun testExportExcel(context: Context, datas: MutableList<Gas>, commonDialog: CommonDialog?) {
suspend fun testExportExcel(
context: Context,
usbPath: String,
datas: MutableList<Gas>,
commonDialog: CommonDialog?
) {
val allData = mutableListOf<List<SimpleCellValue>>()
datas.forEach {
val row = mutableListOf<SimpleCellValue>()
@ -115,17 +122,17 @@ object TestUtils {
row.add(SimpleCellValue(it.unit))
allData.add(row)
}
val excelPath = ExcelUtils.writeStringListToExcel(allData, context)
// val dataPath = LztekUtil.getLztek()?.usbStoragePath + "/"
// // 直接copy到机身
// val command = arrayOf("remount","cp -r $excelPath $dataPath")
// execCommand(command, true, true)
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context)
commonDialog?.dismiss()
}
fun testExportWarnExcel(context: Context, datas: MutableList<Warning>, loadingDialog: CommonDialog?) {
suspend fun testExportWarnExcel(
context: Context,
usbPath: String,
datas: MutableList<Warning>,
loadingDialog: CommonDialog?
) {
val allData = mutableListOf<List<SimpleCellValue>>()
datas.forEach {
val row = mutableListOf<SimpleCellValue>()
@ -135,11 +142,7 @@ object TestUtils {
row.add(SimpleCellValue(it.unit))
allData.add(row)
}
val excelPath = ExcelUtils.writeStringListToExcel(allData, context, true)
// val dataPath = LztekUtil.getLztek()?.usbStoragePath + "/"
// // 直接copy到机身
// val command = arrayOf("remount","cp -r $excelPath $dataPath")
// execCommand(command, true, true)
val excelPath = ExcelUtils.writeStringListToExcel(allData, usbPath, context, true)
loadingDialog?.dismiss()
}
@ -202,4 +205,24 @@ object TestUtils {
process?.destroy()
}
}
suspend fun testCopyVideo(
context: Context,
usbPath: String,
datas: MutableList<Video>,
loadingDialog: CommonDialog?
) {
val usbVideoPath = usbPath + File.separator + "Video"
val dFile = File(usbVideoPath);
if (!dFile.exists() || !dFile.isDirectory) {
dFile.mkdir()
}
datas.forEach {
FileUtils.copy(
File(it.path).inputStream(),
File(usbVideoPath + "/${it.name}").outputStream()
)
}
loadingDialog?.dismiss()
}
}

@ -8,6 +8,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.common.commonlib.db.DBUtils
import com.common.commonlib.db.entity.Video
import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.TestUtils
import com.yinuo.safetywatcher.databinding.ActivityHistoryVideoBinding
import com.yinuo.safetywatcher.watcher.player.ProVideoActivity
import com.yinuo.safetywatcher.watcher.base.BaseActivity
@ -16,8 +17,10 @@ import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryVideoAdapter
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.DateUtils
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.StorageUtils
import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
@ -40,11 +43,6 @@ class HistoryVideoActivity : BaseActivity() {
if (0 == type) {
queryData()
} else if (1 == type) {
val usbStoragePath = LztekUtil.getLztek()?.usbStoragePath
if (usbStoragePath.isNullOrEmpty() || !File(usbStoragePath).exists()) {
showToast(getString(R.string.no_upan_tip))
return
}
export2Upan()
}
}
@ -128,7 +126,18 @@ class HistoryVideoActivity : BaseActivity() {
}
private fun export2Upan() {
// TODO
val usbPath = StorageUtils.getStoragePath(this@HistoryVideoActivity)
if (usbPath.isNullOrEmpty()) {
showToast(getString(R.string.no_upan_tip))
return
}
showLoadingDialog(R.string.export_data_tip)
GlobalScope.launch {
TestUtils.testCopyVideo(this@HistoryVideoActivity, usbPath, mAdapter._data, loadingDialog)
launch(Dispatchers.Main) {
showToast(getString(R.string.export_success))
}
}
}
fun gotoPlayVideo(path: String) {

@ -18,6 +18,8 @@ import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.ChartBridge
import com.yinuo.safetywatcher.watcher.utils.DateUtils
import com.yinuo.safetywatcher.watcher.utils.StorageUtils
import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -204,7 +206,17 @@ class QueryDataActivity : BaseActivity() {
}
private fun doExportData() {
showLoadingDialog(R.string.export_data_tip)
TestUtils.testExportExcel(this, mAdapter._data, loadingDialog)
val usbPath = StorageUtils.getStoragePath(this@QueryDataActivity)
if (usbPath.isNullOrEmpty()) {
showToast(getString(R.string.no_upan_tip))
return
}
showLoadingDialog(R.string.export_data_tip, false)
lifecycleScope.launch {
TestUtils.testExportExcel(this@QueryDataActivity, usbPath, mAdapter._data, loadingDialog)
launch(Dispatchers.Main) {
showToast(getString(R.string.export_success))
}
}
}
}

@ -14,6 +14,8 @@ import com.yinuo.safetywatcher.watcher.constant.DEFAULT_QUERY_TIME_INTERVAL
import com.yinuo.safetywatcher.watcher.ui.adapter.WarnDataAdapter
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.DateUtils
import com.yinuo.safetywatcher.watcher.utils.StorageUtils
import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -118,7 +120,17 @@ class WarnDataActivity : BaseActivity() {
}
private fun doExportData() {
showLoadingDialog(R.string.export_data_tip)
TestUtils.testExportWarnExcel(this, mAdapter._data, loadingDialog)
val usbPath = StorageUtils.getStoragePath(this@WarnDataActivity)
if (usbPath.isNullOrEmpty()){
showToast(getString(R.string.no_upan_tip))
return
}
showLoadingDialog(R.string.export_data_tip, false)
lifecycleScope.launch {
TestUtils.testExportWarnExcel(this@WarnDataActivity, usbPath, mAdapter._data, loadingDialog)
launch(Dispatchers.Main){
showToast(getString(R.string.export_success))
}
}
}
}

@ -0,0 +1,48 @@
package com.yinuo.safetywatcher.watcher.utils
import android.content.Context
import android.os.storage.StorageManager
import android.os.storage.StorageVolume
import java.lang.reflect.InvocationTargetException
object StorageUtils {
/**
* 6.0获取外置sdcard和U盘路径并区分
* @param mContext
* @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘"
* @return
*/
fun getStoragePath(mContext: Context, keyword: String = "USB"): String? {
var targetpath: String? = ""
val mStorageManager = mContext
.getSystemService(Context.STORAGE_SERVICE) as StorageManager
var storageVolumeClazz: Class<*>? = null
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume")
val getVolumeList = mStorageManager.javaClass.getMethod("getVolumeList")
val getPath = storageVolumeClazz.getMethod("getPath")
val result: Array<StorageVolume> =
getVolumeList.invoke(mStorageManager) as Array<StorageVolume>
val length: Int = result.size
val getUserLabel = storageVolumeClazz.getMethod("getUserLabel")
for (i in 0 until length) {
val storageVolumeElement: StorageVolume = result[i]
val userLabel = getUserLabel.invoke(storageVolumeElement) as String
val path = getPath.invoke(storageVolumeElement) as String
if (userLabel.contains(keyword)) {
targetpath = path
}
}
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return targetpath
}
}

@ -94,6 +94,7 @@ object ExcelUtils {
fun writeStringListToExcel(
allRowsData: List<List<ICellValue>>,
usbPath: String,
context: Context,
isWarnData: Boolean = false
): String? {
@ -102,7 +103,7 @@ object ExcelUtils {
"气体浓度_${PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN)}.xls"
else "告警数据_${PathUtils.getNowTimeFormat(PathUtils.DATE_TO_STRING_LONG_PATTERN)}.xls"
val filePath =
PathUtils.getExternalStoragePath(context) + File.separator + fileName
usbPath + File.separator + fileName
val columns = context.resources.getStringArray(R.array.excel_column);
initExcel(columns, filePath, PathUtils.SHEET_NAME) //需要写入权限

@ -24,7 +24,6 @@ object PathUtils {
* 获取应用中文件存储
*/
fun getExternalStoragePath(context: Context): String? {
// return LztekUtil.getLztek()?.usbStoragePath
return context.getExternalFilesDir(null)?.path
}

@ -55,6 +55,7 @@
<string name="over_max_interval_tip">开始时间和结束时间间隔不能超过24小时请调整</string>
<string name="error_interval_tip">开始时间必须小于结束时间!请调整</string>
<string name="no_upan_tip">请先插入U盘!</string>
<string name="export_success">导出完成!</string>
<string name="sync_data_tip">数据同步中</string>
<string name="export_data_tip">数据导出中</string>

@ -13,8 +13,8 @@ project.ext {
versions = [
compileSdkVersion : 33,
buildToolsVersion : "33.0.3",
minSdkVersion : 26,
targetSdkVersion : 28,
minSdkVersion : 30,
targetSdkVersion : 30,
versionCode : 1,
versionName : "1.0",
jvmTarget : "1.8",

Loading…
Cancel
Save