1. Are you ready for the Galaxy S20? Here is everything we know so far!

call read and write file in class from fragment over mainActivity

Discussion in 'Android Development' started by alex_92, Oct 26, 2014.

  1. alex_92

    alex_92 Lurker
    Thread Starter

    hi,
    after wasting a complete evening of useless trying I really need some help.
    I have an Activity with fragments and want to write and read files by clicking an a button or any other event from a fragment.
    The problem is, that I have to place the read and write methods in the mainActivity to get all variables. But I can't call them because the static/non-static problem. I don't know how to fix this. And when I try to put the mothods to a extern class it doesn't compile because "openFileOutput()" is undefined. Here is my code that worked in an app without fragments.

    [HIGH]
    public void writeData (String s)
    {
    BufferedWriter bufferWriter = null;
    try {
    FileOutputStream fileOutputStream = openFileOutput("testFile", Context.MODE_PRIVATE);
    bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
    bufferWriter.write(s);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    bufferWriter.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    public String readData()
    {
    BufferedReader bufferReader = null;
    StringBuilder result = new StringBuilder();
    try {
    FileInputStream fileInputStream = openFileInput("testFile");
    bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
    String line;
    while ((line = bufferReader.readLine()) != null) {
    result.append(line + "\r\n");
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    bufferReader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    Log.d("fileAccess", "" + result.toString());
    return (result.toString());
    }
    [/HIGH]
     



    1. Download the Forums for Android™ app!


      Download

       
  2. deprogram

    deprogram Lurker

    There are probably an infinite number of ways to solve this problem, but here's the absolute simplest:

    Put this class anywhere in the project.

    Then call it like this, from within your activity or fragment:
    The application context should be appropriate for what you're trying to do.

    I wish this forum didn't have broken code formatting. Really annoying.
     
  3. alex_92

    alex_92 Lurker
    Thread Starter

    first thank you, for your hep.
    but your way isn't possible.
    Because the file access methods like openFileOutput need the context of the main-activity. But I can't pass the context from a static function.
    This was my problem.
    But I solved it.
    I put a variable in the file access class to store the context, which is passed at the onCreate of the main-activity over the constructor of the class.
    This way I don't have to pass the context when calling the file access methods.
    Now I can read and write files from fragment events.
     
Loading...
Similar Threads - call read write
  1. becauseWhyNot
    Replies:
    0
    Views:
    128
  2. quocanh59
    Replies:
    2
    Views:
    270
  3. craiger4233
    Replies:
    2
    Views:
    263
  4. Milo Williamson
    Replies:
    3
    Views:
    330
  5. rj sandy
    Replies:
    5
    Views:
    556
  6. Degenhard
    Replies:
    0
    Views:
    312
  7. Dcollins0777
    Replies:
    5
    Views:
    498
  8. Walker61
    Replies:
    0
    Views:
    302
  9. Jodi Holland
    Replies:
    2
    Views:
    632
  10. Scomer88
    Replies:
    7
    Views:
    610

Share This Page

Loading...