[英]How to solve error about saving Android Kotlin file
如何解決? 我創建了一個程序,定期創建手機傳感器數據並將其存儲在手機下載文件夾中的一個文件中。 第一次運行沒問題,但是如果要再次新建文件,刪除path文件夾下的文件再運行,就會出現EExit錯誤。 以文件已經存在於 /storage/emulator/0/~~ 路徑中的方式。 我應該怎么做才能修復此錯誤? 寫入文件的代碼如下。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setChart()
checkFunction()
val saveButton: Button = findViewById(R.id.btn_save)
saveButton.setOnClickListener{
if(isSaving){
// Stop saving data
isSaving = false
saveButton.text = "Save Start"
// Display a toast message to confirm that the data was saved
Toast.makeText(this, "Stop Sensor data saved to file", Toast.LENGTH_SHORT).show()
} else {
// Start saving data
isSaving = true
saveButton.text = "Save Stop"
// Check if app has permission to write to external storage
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
//Request permission if it doesn't already have it
ActivityCompat.requestPermissions(this, permission_list, 1)
} else {
// Display a toast message to confirm that the data was saved
Toast.makeText(this, "Start Sensor data saved to file", Toast.LENGTH_SHORT).show()
// Define file
saveCounter++
// if permission is already granted, start saving sensor data in a background thread
GlobalScope.launch(Dispatchers.IO) {
while(isSaving){
saveSensorDataToFile()
delay(1000)
}
}
}
}
}
}
// -------------------- Save Data at File -------------------
private fun saveSensorDataToFile() {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
) {
// Get the path to the exteranl storage
val root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val dir = File(root.path + "/SensorData3")
dir?.mkdirs()
val fileName = "Sensor_data_$saveCounter.txt"
val file = File(dir, fileName)
val sdf = SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault())
val currentDate = sdf.format(Date())
val data = "$currentDate, $pre_x, $acc_x, $acc_y, $acc_z\n"
file.appendText(data)
} else {
// Permission not granted, request permission
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
0
)
}
}
// 권한 확인 함수
private fun checkFunction() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
&& ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)
&& ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE), 101)
} else {
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE), 101)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_acc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="acc_value"
android:textSize="20dp"
android:textAlignment="center"
android:gravity="center"/>
<TextView
android:id="@+id/tv_pressure"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="pressure_value"
android:textSize="20dp"
android:textAlignment="center"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart_accX"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart_accY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart_accZ"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"/>
</LinearLayout>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart_pressure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_save"
android:text="Save Start"
android:textSize="20dp"/>
</LinearLayout>
val file = File(dir, fileName)
var fileExists = file.exists()
if(fileExists){
// file already there. so delete and new here
} else {
// create new file
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.