Skip to content

Instantly share code, notes, and snippets.

@nosix
Last active November 17, 2022 14:17
Show Gist options
  • Save nosix/532c51d3568d90dfba5222edb1e689d8 to your computer and use it in GitHub Desktop.
Save nosix/532c51d3568d90dfba5222edb1e689d8 to your computer and use it in GitHub Desktop.
Writting to external storage (SD card) for Android (SDK 23) in Kotlin 1.0.3.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="xxx">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
package xxx
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
class MainActivity : AppCompatActivity() {
companion object {
const val REQUEST_PERMISSION = 1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION)
} else {
write()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_PERMISSION -> if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
write()
}
}
}
private fun write() {
val dir = "${Environment.getExternalStorageDirectory()}/$packageName"
File(dir).mkdirs()
val file = "%1\$tY%1\$tm%1\$td%1\$tH%1\$tM%1\$tS.log".format(Date())
File("$dir/$file").printWriter().use {
it.println("text")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment