In my android app I can't access my json file in my assets folder. It keeps giving me a FileNotFoundException.
Without reading it directly into a InputStream I want to get the location of the json file instead. I have tried using /main/assets/data.json or ///android_assets/data.json or /assets/data.json.
The main purpose of this is to pass a string that holds the location of the json file to another class.
Is there a way to have the location of the json file in the assets file be returned to me? Is there anywhere I can check to make sure my app recognizes that the assets folder is even a part of the application?
EDIT:
In my Activity
public void loadData(){ String location = "file:///android_assets/data.json"; JSONHandler json = new JSONHandler(); List<ListItemObject> list = json.getJSONFile(location); } In my JSONHandler Class
public ListItemObject getJSONFile(String Location){ File file = new File(address); InputStream in; InputStreamReader isr; JsonReader jr; List<SMRListItemObject> messages = null; try{ in = new FileInputStream(file); isr = new InputStreamReader(in,"UTF-8"); jr = new JsonReader(isr); messages = readMessagesArray(jr); jr.close(); isr.close(); in.close(); } catch (FileNotFoundException e) { Log.d("Exception", "File Not Found"); } catch (IOException e) { Log.d("Exception", "IO problem"); } return messages; } The line where I am calling the getJSONFile is where I am getting the FileNotFoundException being thrown.
Error message:
02-26 02:38:01.086 27165-27165/com.example.generalcounsel.sm D/OpenGLRenderer﹕ Enabling debug mode 0 02-26 02:38:01.817 27165-27165/com.example.generalcounsel.sm D/Exception﹕ File Not Found 02-26 02:38:01.817 27165-27165/com.example.generalcounsel.sm D/AndroidRuntime﹕ Shutting down VM 02-26 02:13:59.690 25369-25369/com.example.generalcounsel.sm W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418a5da0) 02-26 02:13:59.690 25369-25369/com.example.generalcounsel.sm E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.generalcounsel.sm, PID: 25369 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.generalcounsel.sm/com.example.generalcounsel.sm.ListActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) at android.app.ActivityThread.access$900(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.generalcounsel.sm.ListActivity.loadData(ListActivity.java:72) at com.example.generalcounsel.sm.ListActivity.onCreate(ListActivity.java:51) at android.app.Activity.performCreate(Activity.java:5426) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)