0

当我打开应用程序时,必须从外部读取应用程序并将联系人中的所有号码复制到该文件,但是当我尝试总是强制关闭时。

public class anti_spam extends TabActivity {
    /** Called when the activity is first created. */
    BufferedReader reader;
    public String[] Number;
    int x=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //------------------------
        getListNumber();
        File logFile = new File("root/log.txt");

        //Hitung Jumlah isi File
        try {
            reader = new BufferedReader(new FileReader("root/log.txt"));
                try {
                while (reader.readLine()!= null)
                {
                    x++;
                }
                reader.close();
            } catch (IOException e) {}
        } catch (FileNotFoundException e1) {}

        //Hapus File Jika Jumlah Kontak Beda
        if (x-1 != Number.length){
           logFile.delete();
        }
           if (!logFile.exists())
           {
              try
              {
                 logFile.createNewFile();
                 Toast.makeText(getBaseContext(), "Data Berhasil Dibuat", Toast.LENGTH_LONG).show();
                 //Isi File
                   try
                   {
                      //BufferedWriter for performance, true to set append to file flag
                      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
                      String[] numTemp;
                      String numBaru;
                      for(int i=0;i<Number.length;i++)
                      {
                          numTemp = Number[i].split("-");
                        numBaru = (numTemp[0]+numTemp[1]+numTemp[2]);
                        if (numBaru.substring(0, 1).equals("0"))
                        {
                            numBaru = numBaru.replaceFirst("0","+62");
                        }
                          buf.append(numBaru + "\n");
                      }
                      buf.newLine();
                      buf.close();
                   }
                   catch (IOException e)
                      {
                       Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
                      }
              } 
              catch (IOException e)
              {
                 // TODO Auto-generated catch block
                  Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
              }
           }}

在清单上

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

2 回答 2

0

@Dhiren 关于向清单文件添加权限是正确的:

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

但是还有更多问题,所以请继续阅读......


我认为您可能需要更加注意允许写入的文件夹。

查看Environment.getExternalStorageDirectory()上的此链接,了解如何执行此操作。

这些答案中还有一些有用的代码:

于 2013-06-28T13:15:43.963 回答
0

请将此添加到清单文件中。

    <uses-permission android:name="android.permission.READ_CONTACTS" />
于 2013-06-11T05:01:35.300 回答