0

我正在尝试在我的 sd 卡上备份短信并使其正常工作,但是当我进行备份时出现一个问题,它删除了以前存储的文件。但我想将所有文件保留在我的 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();
           }

        }
    });  

请帮助我,我是 android 新手。提前致谢

4

2 回答 2

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 创建一个文件

于 2013-02-13T14:53:07.747 回答
1

如果我理解您的要求正确,您的 SMS.csv 文件将被覆盖,并且您希望继续添加其他文件而不是覆盖此文件。

线

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

需要更改为类似

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

这应该每次都创建一个新的备份文件。

于 2013-02-13T13:40:20.873 回答