2013-02-13 86 views
0

我想備份我的SD卡上的短信及其工作,但有一個問題,當我正在進行備份時,刪除以前存儲的文件。但我想保留我的SD卡上的所有文件。我正在使用此代碼如何在Android SD卡上寫

backup=(Button)findViewById(R.id.backup); 
    backup.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      backupSMS(); 
     } 
      public ArrayList<String> smsBuffer = new ArrayList<String>(); 
       String smsFile = "SMS"+".csv"; 



     private void backupSMS() { 
      smsBuffer.clear(); 
      Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox"); 
      Cursor cursor1 = getContentResolver().query(
        mSmsinboxQueryUri, 
        new String[] { "_id", "thread_id", "address", "person", "date", 
          "body", "type" }, null, null, null); 
      //startManagingCursor(cursor1); 
      String[] columns = new String[] { "_id", "thread_id", "address", "person", "date", "body", 
        "type" }; 
      if (cursor1.getCount() > 0) { 
       String count = Integer.toString(cursor1.getCount()); 
       Log.d("Count",count); 
       while (cursor1.moveToNext()) { 

        String messageId = cursor1.getString(cursor1 
          .getColumnIndex(columns[0])); 

        String threadId = cursor1.getString(cursor1 
          .getColumnIndex(columns[1])); 

        String address = cursor1.getString(cursor1 
          .getColumnIndex(columns[2])); 
        String name = cursor1.getString(cursor1 
          .getColumnIndex(columns[3])); 
        String date = cursor1.getString(cursor1 
          .getColumnIndex(columns[4])); 
        String msg = cursor1.getString(cursor1 
          .getColumnIndex(columns[5])); 
        String type = cursor1.getString(cursor1 
          .getColumnIndex(columns[6])); 



        smsBuffer.add(messageId + ","+ threadId+ ","+ address + "," + name + "," + date + " ," + msg + " ," 
          + type); 

       }   
       generateCSVFileForSMS(smsBuffer); 
      }    
     } 


     private void generateCSVFileForSMS(ArrayList<String> list) 
     { 

      try 
      { 
       String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + smsFile; 
       FileWriter write = new FileWriter(storage_path); 

       write.append("messageId, threadId, Address, Name, Date, msg, type"); 
       write.append('\n'); 
       write.append('\n'); 


       for (String s : list) 
       { 
        write.append(s); 
        write.append('\n'); 
       } 
       write.flush(); 
       write.close(); 
      } 

      catch (NullPointerException e) 
      { 
       System.out.println("Nullpointer Exception "+e); 
       // e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 
    }); 

請幫助我我新來的機器人。在此先感謝

+0

一個文件你的意思是你要多個備份,在這種情況下,你可以給所有的備份不同的名稱(可能使用當前時間),然後保存在名爲'smsbackups'的文件夾中,或者進行恢復,只需將該文件夾中的所有文件添加到列表中,然後用戶選擇要恢復的文件? – jtwigg 2013-02-13 13:41:04

回答

1

如果我明白你在問什麼問題糾正你的SMS.csv文件被覆蓋,你想繼續添加額外的文件,而不是寫這個文件。

String smsFile = "SMS"+".csv";

需要被改變成類似

String smsFile = "SMS-" + SystemClock.currentThreadTimeMillis() + ".csv"; 

這每一次應該創建一個新的備份文件。

+0

哇,它的工作。謝謝你從我的心底。但我仍然有一個問題,它沒有在SD卡上創建文件夾,請提前幫助我謝謝 – IPL10 2013-02-13 14:17:59

2

我無法發佈代碼作爲您要求創建目錄的註釋。

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
    "/" + getPackageName() + 
    "/SMS-" + System.currentTimeMillis() + 
    ".csv"); 

if(file.mkdirs()) { 
    // your code here for writing the file 
    // your file is now at /sdcard/your.apps.package.name/sms-currenttime.csv 
} 

這應該創建一個在/sdcard/your.apps.package.name/sms-currenttime.csv

+0

是的,它的工作....再次感謝我的心底 – IPL10 2013-02-14 07:59:50