Search code examples
androidjsonkotlinandroid-manifestgeojson

How to download GeoJSON file in Kotlin?


So the GeoJSON file will be created with the following code:

val coordinates = listOf(
Coordinate(4.5165132153, 99.651681683),
Coordinate(4.5163152153, 99.878981683),
Coordinate(4.3584532153, 99.021481683),
Coordinate(4.1578132153, 99.698781683),
Coordinate(4.3655132153, 99.321481683),
Coordinate(4.8795132153, 99.874581683)
)

val geometry = mapOf(
    "type" to "Polygon",
    "coordinates" to listOf(listOf(coordinates.map { listOf(it.lon, it.lat) }))
)

val properties = mapOf(
    "name" to "Your Polygon Name",
    // Add other properties as needed
)

val feature = mapOf(
    "type" to "Feature",
    "geometry" to geometry,
    "properties" to properties
)

val featureCollection = mapOf(
    "type" to "FeatureCollection",
    "features" to listOf(feature)
)

Then serialise the GeoJSON data into JSON string

val gson = Gson()
val geoJsonString = gson.toJson(featureCollection)

Then write the JSON string in the app's internal storage

val fileName = "my_polygon.geojson"
val file = File(context.filesDir, fileName)

file.writeText(geoJsonString)

So I would like to download this data and save it into the phone storage by clickling a button, for example: binding.downloadButton.setOnClickListener{ }

Does anyone know how to perform this task?

Thank you!


Solution

  • First, define the required permissions.

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

    Check permissions.

    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
           == PackageManager.PERMISSION_DENIED) {
        requestPermissions(
            arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
            PERMISSION_REQUEST_CODE
        )
    }
    

    And save file operation

    try {
        val root =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
        if (!root.exists()) {
            root.mkDirs()
        }
        val file = File(root, "fileName.json")
        val writer = FileWriter(file)
        writer.append(geoJsonString)
        writer.flush()
        writer.close()
    } catch (e: Exception) {
        e.printStackTrace()
    }
    

    Your case

    binding.downloadButton.setOnClickListener {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_DENIED) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                PERMISSION_REQUEST_CODE
            )
        } else {
            try {
                val root =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
                if (!root.exists()) {
                    root.mkdirs()
                }
                val file = File(root, "fileName.json")
                val writer = FileWriter(file)
                writer.append(geoJsonString)
                writer.flush()
                writer.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }