0

我使用下面的代码将文件写入 sd 卡并读取它的内容:

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(); 
     }

问题是:我如何写入手机内存并从中读取?

4

2 回答 2

0

您需要 root 手机才能获得对任何手机文件夹的写入权限。

如果您想要在特定于应用程序的内部存储中写入,那么您可以遵循本指南:http: //developer.android.com/guide/topics/data/data-storage.html#filesInternal

于 2011-05-16T21:48:19.980 回答
0

首先,您必须在清单文件中放入这一行:

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

如果还不够,你能更详细地解释问题吗?

于 2011-05-17T08:56:54.903 回答