如何将短信从SD卡恢复到收件箱?

时间:2013-01-18 08:02:51

标签: android sms restore android-sdcard

我想将收件箱中的所有邮件存储到SD卡中,然后将其恢复并恢复到收件箱。我已成功备份邮件并将其存储到SD卡但我无法弄清楚如何检索它们回到并回复到Inbox。这是我用来备份短信的代码:

private void  backupSMS()
    {
        smsBuffer.clear();
        Uri mSmsinboxQueryUri = Uri.parse("content://sms");
        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 + "Velosys"+".csv";
             FileWriter write = new FileWriter(storage_path);

             write.append("messageId, threadId, Address, Name, Date, msg, type");
             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();
        } 
     }

我搜索了很多,但无法找到从SdCard的.csv文件恢复短信的方法。请帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

不是查询数据库,而是使用bulkInsert的insert插入行。我相信你需要在清单中请求对该数据库的写权限。