Socket
Socket
Sign inDemoInstall

ru.bartwell:safhelper

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ru.bartwell:safhelper

Open source Android library for files writing on SD card.


Version published
Maintainers
1
Source

SafHelper

Open source Android library for files writing on SD card.

Features

SafHelper using Storage Access Framework (SAF) to retrieve permission, create directories and write files on SD card.

Integration

Add dependency in build.gradle:

compile 'ru.bartwell:safhelper:1.0.0'

Usage

private lateinit var safHelper: SafHelper

private fun onButtonClick() {
    writeFile(true)
}

private fun writeFile(requestPermissions: Boolean) {
    val userFile = File(main_path.text.toString())

    if (userFile.parent == null) {
        showToast("Wrong path")
    } else {
        safHelper = SafHelper(this, userFile.parent)
        if (safHelper.isApplicable()) {
            if (safHelper.isPermissionGranted()) {
                try {
                    if (safHelper.mkdirs(userFile.parent)) {
                        val outputStream = safHelper.createFile(userFile.path)
                        outputStream.use {
                            it.write("Text in the file".toByteArray())
                        }
                        showToast("Success")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    showToast("Error: " + e.message)
                }
            } else {
                if (requestPermissions) {
                    safHelper.requestPermissions(this)
                } else {
                    showToast("Permissions not granted")
                }
            }
        } else {
            showToast("File is not on SD Card or Android version < 5.0")
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
    if (safHelper.onActivityResult(this, requestCode, resultCode, resultData)) {
        writeFile(false)
    }
}

License

Copyright © 2018 Artem Bazhanov

SafHelper is provided under an Apache 2.0 License.

FAQs

Package last updated on 20 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc