Question

i use the code below to write a file to the sd card and read the content of it:

try {
        if (root.canWrite())
        {
                File rootdir = Environment.getExternalStorageDirectory();
                File yourFile = new File(rootdir, "tomato50.txt");
                FileWriter filewriter = new FileWriter(file,true);                                                                     
                BufferedWriter out = new BufferedWriter(filewriter);
            for (int k=0; k<assignArr.size(); k++)
            {
               out.write(assignArr.get(k) + "\n");
               Toast.makeText(MainActivity.this, "out: " + assignArr.get(k), Toast.LENGTH_LONG).show();
            }
            out.close();
          }
          } catch (IOException e) {
          Log.e("TAG", "Could not write file " + e.getMessage());


try { 
       File rootdir = Environment.getExternalStorageDirectory();
       File yourFile = new File(rootdir, "tomato50.txt");
       FileReader filereader = new FileReader(yourFile);
       BufferedReader br = new BufferedReader(filereader);
       String line; 
       while((line = br.readLine()) != null) 
       {
           assignArrBe.add(line); 
           Toast.makeText(MainActivity.this, "Read from file: " + line, Toast.LENGTH_LONG).show();
        } 
      br.close();
       }
      catch (IOException e) 
     { 
        e.printStackTrace(); 
     }

Question is: how can i write to the phone memory and read from it?

Was it helpful?

Solution

You will need to root your phone in order to get write access to any phone folder.

If what you want is to write in the app-specific internal storage, then you can follow this guide: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

OTHER TIPS

First of all you have to put in your manifest file this line:

...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

If it is not enough, can you explain the problem with more details?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top