I'm trying to export data from the app into a text file, and to do that I understand that I need to discover the appropriate path to my app-specific file storage spot. I discovered that I should use something like context.getExternalFilesDir()
. While this seems pretty straightforward, when I try to do val path = context.getExternalFilesDir()
, Android Studio tells me that context
is a function and can't be called with the arguments supplied. Seems like others don't have this problem, is there an import that I'm missing or something along those lines?
Since I had come across this line context.getExternalFilesDir()
to be used to return the path where files should go, I had assumed it would just work, but no such luck. I've dug into it for quite a while but can't seem to figure out what to do in the case where the IDE doesn't want to recognize it.
Here are my imports, in case there's something I'm missing:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateMap
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.times
import com.example.packandfind.ui.theme.PackAndFindTheme
import java.io.File
Here's the function that contains the attempted call:
fun importData(): SnapshotStateMap<String, List<String>> {
val fileName = "data.txt"
// val path = context.getExternalFilesDir()
val file = File(fileName)
var data = mutableStateMapOf<String, List<String>>()
if (file.exists())
{
file.forEachLine {
val (key, value) = it.split("=")
// var newValue = value.replace("[", "")
// newValue = newValue.replace("]", "")
// newValue = newValue.replace(" ", "")
val listValue = value.split(",")
data[key] = listValue
}
} else {
data = mutableStateMapOf("New box" to listOf("location"))
}
return data
}
CodePudding user response:
I would suggest to pass the context as a parameter from the activity you called from and make your function accept it.
fun importData(context: Context): SnapshotStateMap<String, List<String>> {
....
}
CodePudding user response:
You appear to be attempting to use Compose UI. In that case, a @Composable
function can use LocalContext.current
to obtain a valid Context
:
@Composable
fun FruitText(fruitSize: Int) {
// Get `resources` from the current value of LocalContext
val resources = LocalContext.current.resources
val fruitText = remember(resources, fruitSize) {
resources.getQuantityString(R.plurals.fruit_title, fruitSize)
}
Text(text = fruitText)
}
(from the docs)
Since you seem to be unfamiliar with Context
... I strongly recommend that either:
- You learn conventional Android app development (using the
View
-based UI system) via some book or course before taking on Compose UI, or - You find some book or course that teaches you Android app development using Compose UI