Reputation: 941
I want to store all the messages from inbox to sdcard and later retrieve and restore them to the inbox.I have been successful in taking backup of the messages and store them to sdcard but am not able to figure out how to retrieve them back and retore to Inbox.This is the code i am using to take backup of the sms :
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();
}
}
I searched a lot but not able to find a way to restore the sms from .csv file of SdCard.Please help me.Thanks in advance.
Upvotes: 1
Views: 1930
Reputation: 93668
Instead of querying the DB, you insert rows into it using insert of bulkInsert. I believe you need to request write permissions to that database as well in your manifest.
Upvotes: 1