Jetpack InternalStorage

Jetpack InternalStorage

Jlozorik

Ну, так-то ничего сложного, главное ПРАВИЛЬНО передать Context from MainActivity to any Composable function, sooo:

MainActivity:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            StorageTheme {
                Surface(modifier = Modifier
.fillMaxSize(), color = MaterialTheme.colorScheme.background){
            // code here
            YourComposeFun(context = LocalContext.current, data)

Compose Fun:

@Composable
fun YourComposeFun(context : Context, data : Any?){
  var data by remember {mutableStateOf(" ")}
  //some content
  //Button for SAVE DATA
  Button(
    onClick = {
        try {
            val fos: FileOutputStream =
                context.openFileOutput("demoFile.txt", Context.MODE_PRIVATE)
            fos.write(message.value.toByteArray())
            fos.flush()
            fos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
)
// Button For READ DATA
Button(
    onClick = {
        try {
            val fin: FileInputStream = context.openFileInput("demoFile.txt")
            var a: Int
            val temp = StringBuilder()
            while (fin.read().also { a = it } != -1) {
                temp.append(a.toChar())
            }
            data = temp.toString()
            fin.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
)
}


Report Page