如何从FragmentActivity中的内部存储中检索数据?

时间:2014-03-12 09:54:48

标签: java android android-fragments internal-storage

大家好我正在开发一个将JSON数据写入内部存储的Android应用程序,它应该从扩展 FragmentActivity 中检索。

这是关于如何将数据写入内部存储的代码:

private void createFile(String data) throws IOException{
    FileOutputStream fos = openFileOutput("userObj", Context.MODE_PRIVATE);
    fos.write(data.getBytes());
    fos.close();
}

当我从检索扩展活动的数据时 我 CAN 读取这样的数据

private void readFile() throws IOException, JSONException {

     FileInputStream fis = openFileInput("userObj");
     BufferedInputStream bis = new BufferedInputStream(fis);
     StringBuffer b = new StringBuffer();

     while (bis.available() != 0){
        char c = (char) bis.read();
        b.append(c);
     }

     bis.close();
     fis.close();
     JSONObject data = new JSONObject(b.toString());
     Log.d(Constants.DATA,  data.getString("qr_code_png"));
}

但是当我从检索数据时,使用相同的代码扩展 FragmentActivity 无法检索

  public void readFile() throws IOException, JSONException {
        FileInputStream fis = openFileInput("userObj");
        BufferedInputStream bis = new BufferedInputStream(fis);
        StringBuffer b = new StringBuffer();
        while (bis.available() != 0){
            char c = (char) bis.read();
            b.append(c);
        }
        bis.close();
        fis.close();
        JSONObject data = new JSONObject(b.toString());
        Log.d(Constants.DATA,  data.getString("qr_code_png"));

    }

如何从扩展片段活动的类中检索内部存储中保存的数据?

0 个答案:

没有答案