0

I am trying to import a local JSON file into an Android app using Kotlin and Android Studio, however the app can not seem to find the file. Even with an explicit path the JVM can not seem to get at the file. I have tried making a .txt file, and JVM can not find that either. Klaxon's tutorials do not work because the Parser class has been deprecated; which doesn't matter anyway because that's not where the error is. And yes, the file exists in the directory, and it is spelled that way.

fun parse(name: String) : Any? {
    val inputStream: InputStream = File("C:\\Users\\Athena\\AndroidStudioProjects\\AD340HW1\\app\\zombie_movies.json").inputStream()
    val lineList = mutableListOf<String>()

    //val cls = Parser::class.java
    //inputStream.bufferedReader().useLines { lines -> lines.forEach {lineList.add(it)  } }
    //val strArray = arrayOfNulls<String>(lineList.size)
    return lineList
 }

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.athena.ad340hw1/com.example.athena.ad340hw1.ZombieMovies}: java.io.FileNotFoundException: C:\Users\Athena\AndroidStudioProjects\AD340HW1\app\zombie_movies.json (No such file or directory)

  • 1
    An Android app runs on an Android device (real or virtual) while the path refers to a Windows system (probably the development system you work with). An Android app can't access this. – Michael Butscher Apr 20 '18 at 22:27
  • 1
    That makes sense. Any advise on how to import data into an app? I would rather not hard code 20 zombie movies, and their descriptions. – Nicholas Bennett Apr 20 '18 at 22:47
  • If it is just a fix file (or multiple), start here: https://stackoverflow.com/questions/12037069/what-is-an-android-asset – Michael Butscher Apr 20 '18 at 22:52
  • Honestly, rtfm. Do you have any idea how to add resources to an Android app? There is a metric tonne of online documentation about this. – AutonomousApps Apr 21 '18 at 04:27
  • People who give non-answers tend to do so because they don't know the answer. Why don't we read those obsolete documents together? – Nicholas Bennett Apr 21 '18 at 17:34

2 Answers2

0

To use a text file or any other external file you have to have the android app ask the user to grant permission. There is code out there that will popup a box asking for permission to use the file system. Here is a link to get you started I'm sure there are many other good tutorials on the subject. https://developer.android.com/training/permissions/requesting.html

calmchess
  • 118
  • 8
0

So what I ended up doing is just hosting the JSON on a server, and pulling it off the server. That ended up being better then working with it on my local machine, because in the future that data needs to be accessed from a remote device. I guess my root error was in trying to proceed too slowly, too methodically; I should have just fired up the server that I needed and been done with it.