Android导入和导出数据

2022-10-17  本文已影响0人  BlueSocks

关于数据,占据我们思想的一件非常重要的事情就是不要丢失它。我们需要备份数据以在需要时恢复数据。因此,我们需要知道如何将数据推送到设备的SD存储器。在本文中,我们将了解如何从 JSON 文件导入和导出数据。

要导出和导入数据,我们将使用 Gson 库。它是一个将 Java 对象转换为其 JSON 表示形式的 Java 库。此外,它还将 JSON 字符串转换为等效的 Java 对象。

要进行设置:请执行以下操作:

implementation 'com.google.code.gson:gson:2.8.9'

首先,创建一个简单的UI用于学习。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnExport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="-6dp"
        android:layout_marginTop="65dp"
        android:layout_marginBottom="20dp"
        android:text="Export"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnImport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnExport"
        android:layout_centerInParent="true"
        android:text="Import"
        android:textSize="20sp" />
</RelativeLayout>

然后,让我们回到代码。

1. 将数据导出到 JSON 文件

通常,我们从某个数据源获取数据。
在我们的示例中,我们将创建虚假数据,这将是客户和员工的两个列表。

创建客户类

class Customer(var name: String, var job: String, var overDue: Float)

创建员工类

class Employee(var nameEmployee: String, var jobEmployee: String)

创建用于存储这两个列表的数据类

class Data(val customers: List<Customer>, val employees: List<Employee>)

使用虚拟数据创建两个列表

 val customers = ArrayList<Customer>()
        customers.add(Customer("Hala", "developer", 34.5f))
        customers.add(Customer("Nora", "teacher", 43.5f))
        customers.add(Customer("Rana", "doctor", 56.5f))

        val employees = ArrayList<Employee>()
        employees.add(Employee("ALi", "carpenter"))
        employees.add(Employee("Yaser", "mechanic"))

初始化 Gson 对象并使其全局化以便能够访问它

private var gson: Gson? = null
gson = GsonBuilder().serializeNulls().setPrettyPrinting().create()

将类转换为 JSON 格式

private fun convertClassToJson(customers: List<Customer>, employees: List<Employee>): String? {
    val allData = Data(customers, employees)
    return gson?.toJson(allData)
}

添加写入和读取数据的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

检查是否已授予存储权限。

private fun isStoragePermissionGranted(): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //Permission is granted
            true
        } else {
            //Permission is revoked
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
            false
        }
    } else {
        // Permission is automatically granted on sdk<23 upon installation
        true
    }
}

获取随机文件名,而不是覆盖相同的文件名

private fun getRandomFileName(): String {
    return Calendar.getInstance().timeInMillis.toString() + ".json"
}

将数据写入 JSON 文件

private fun writeTextToFile(jsonResponse: String?) {
    if (jsonResponse != "") {
        // Create a File object like this.
        val dir = File("//sdcard//Download//")
        val myExternalFile = File(dir, getRandomFileName())
        // Create an object of FileOutputStream for writing data to myFile.txt
        var fos: FileOutputStream? = null
        try {
            // Instantiate the FileOutputStream object and pass myExternalFile in constructor
            fos = FileOutputStream(myExternalFile)
            // Write to the file
            fos.write(jsonResponse?.toByteArray())
            // Close the stream
            fos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        Toast.makeText(this@MainActivity, "Information saved to SD card. $myExternalFile", Toast.LENGTH_SHORT).show()
    }
}

使用导出按钮备份数据

binding.btnExport.setOnClickListener {
    if (isStoragePermissionGranted()) {
        writeTextToFile(allJsonResponse)
    }
}

现在,您的数据将导出到手机下载文件夹中的JSON文件。

2. 从 JSON 文件导入数据

打开文件管理器以选择要导入的 JSON 文件

private fun openFileManager() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.type = "*/*"
    getDataFromFile.launch(intent)
}

从 JSON 文件中读取数据

private fun readTextFromUri(uri: Uri): String {
    var inputStream: InputStream? = null
    val stringBuilder = StringBuilder()
    try {
        inputStream = contentResolver.openInputStream(uri)
        val reader = BufferedReader(InputStreamReader(inputStream))

        // Read a line of text.
        var line = reader.readLine()
        // Read the entire file
        while (line != null) {
            // Append the line read to StringBuilder object. Also, append a new-line
            stringBuilder.append(line).append('\n')
            // Again read the next line and store in variable line
            line = reader.readLine()
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return stringBuilder.toString()
}

在活动中获取 JSON 数据

private var getDataFromFile =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        if (result.resultCode == RESULT_OK) {
            val uri = result.data?.data
            val fileContents = readTextFromUri(uri!!)
            Toast.makeText(this@MainActivity, fileContents, Toast.LENGTH_SHORT).show()
        }
    }

使用导入按钮恢复您的数据

binding.btnImport.setOnClickListener {
    openFileManager()
}

恭喜,您的数据是从 JSON 文件导入的,您现在可以将其插入到数据库中,或以适合应用程序用途的任何方式使用它。

更多Android技术交流和学习资料,可以私xin了解。

上一篇 下一篇

猜你喜欢

热点阅读