2

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) 

            

6
  • please put your code to show to where are you making mistake Commented Feb 26, 2015 at 6:48
  • @Vaishali I don't have much code right now. I have a string which is supposed to hold the full path of the json file in my assets folder (this is whats not working). All paths I try like the ones listed above haven't worked. Anyway then the string will be passed to my json parser class for further manipulation. Commented Feb 26, 2015 at 7:10
  • there might be issue other than the path, plz post your code so that the community might be able to help you Commented Feb 26, 2015 at 7:20
  • @Neji edited my post with my code and error message Commented Feb 26, 2015 at 8:03
  • exception is in loadData() post code of the function! Commented Feb 26, 2015 at 9:33

1 Answer 1

3

try opening file using following way

AssetManager assetManager = getAssets(); InputStream ims = assetManager.open("data.json"); 

Also, getAssets() only works with a context, you may need context to call it in class other than activity.

Read json file from assets using following code

public String loadJSONFromAsset() { String json = null; try { InputStream is = getAssets().open("file_name.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } 

and then parse into a json object using following code

JSONObject obj = new JSONObject(json_return_by_the_function); 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway though to get the full path of the json file in terms of a string?
there is no absolute path as assets are stored in apk. Still, you can use file:///android_asset to reference to files in assets

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.