2012-06-18 65 views
2

我想用InputStream變量從資產文件夾中打開JSON文件,但是我得到了FileNotFoundExeption。該文件在那裏,路徑是正確的。我看了全部,但是我在這裏找到的任何東西都不起作用。任何想法,我有什麼錯,以及如何糾正它(代碼請)?來自Assets的Android InputStream文件:FileNotFoundExeption

從主叫活動中:

String uri = "file:///android_asset/html/json/regulatory_list.json"; 
     args.putString("KEY_URL", uri); 

在片段:

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     Bundle args = this.getArguments(); 
     String URL = args.getString(KEY_URL); 
     new GetJSONTask().execute(URL); 
    } 

    class GetJSONTask extends AsyncTask<String, Integer, String> { 

    protected String doInBackground(String... arg0) { 

     String uri = arg0[0]; 

     InputStream is = null; 

     if (uri.contains("http") == true) {// Run from URL 
      try {. . . 

      } else { 

      try {. . . 
       InputStream jsonFile = getActivity().getAssets().open(uri); 
       Reader reader = new BufferedReader(new InputStreamReader(jsonFile, "UTF-8")); 
      . . . 
     } 
    . . . 

回答

2

我覺得你的文件URI是不是你正在嘗試做正確的格式。

嘗試改變這一點:

String uri = "file:///android_asset/html/json/regulatory_list.json"; 

這樣:

String uri = "html/json/regulatory_list.json"; 
+0

啊!我開悟了。我猜.getAssets()已經知道在哪裏看。日Thnx! – CelticParser