0

i am trying to backup sms on my sd card and its working but there is one problem when i am taking a backup its remove the previous stored file. but i want to keep all files on my sdcard. i am using this code

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

        }
    });  

please help me i new to android . thanks in advance

1
  • do you mean that you want several backups, in which case could you give all your backups different names (maybe using the current time) and then save in a folder named 'smsbackups' or something then to do a restore just add all files in that folder to a list and the user selects which file to restore?
    – twigg
    Feb 13, 2013 at 13:41

2 Answers 2

2

I couldn't post code as a comment to what you asked for creating a directory.

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
}

This should create a file at /sdcard/your.apps.package.name/sms-currenttime.csv

0
1

If I understand what you are asking correcty your SMS.csv file is being over-written and you would like to keep adding additional files instead of over writing this file.

The line

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

needs to be changed to something like

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

This should create a new backup file each time.

1
  • wow great its working. thank you from bottom of my heart. but i have still a problem its not making a folder on SD card please help me thanks in advance
    – Ashfaque
    Feb 13, 2013 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.