【问题标题】:Android studio: Can't find obvious file location in c:?Android Studio:在 c: 中找不到明显的文件位置?
【发布时间】:2016-10-20 23:57:57
【问题描述】:

每次运行此代码时,我都会在异常中收到“找不到文件”的消息,但是,我不明白为什么当我真正将要查找的文件放在我的 c 根目录时:?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView testMEtext = (TextView) findViewById(R.id.testMEtext);
    //
    JSONParser parser = new JSONParser();
    try {
        //File temp = new File("C:/Windows/outdoorWeather.json");
        //String path = temp.getAbsolutePath();

        //testMEtext.setText(path);

        **Object obj = parser.parse(new FileReader("/C:/outdoorWeather.json"));**

        JSONObject jsonObject = (JSONObject) obj;

        String oTemp = (String) jsonObject.get("Fahrenheit temperature");
        testMEtext.setText(oTemp);

    }
     catch (FileNotFoundException e) {
        e.printStackTrace();
         testMEtext.setText("File not found");
    }
    catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

}

【问题讨论】:

  • Android 应用程序无法以这种方式从您的计算机中读取文件。您需要在应用程序文件夹中添加资源。它可以在资产文件夹或 res/raw 文件夹中

标签: json android-studio


【解决方案1】:

我认为您无法与模拟器之外的对象进行交互。要解决它,请尝试参考这些。
Load a simple text file in android studio
How can I read a text file in android
Reading a simple text file

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 2018-05-21
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多