2012-01-05 3 views
5

मैं अपने ऐप में बैकअप एसएमएस और संपर्कों को एसडी कार्ड में .xml या .csv प्रारूप के रूप में विकसित करने की कोशिश कर रहा हूं और इसे बाद में बहाल करें।एसडी कार्ड में .xml फ़ाइल या .csv फ़ाइल के रूप में संपर्कों या एसएमएस को बैकअप कैसे लें और इसे बाद में पुनर्स्थापित करें

तो कृपया कोई मुझे कुछ सुझाव या कुछ नमूना कोड या इससे संबंधित किसी भी संसाधन लिंक दें। अग्रिम

+0

mmh चूंकि आप बैकअप का उपयोग स्वयं लिखते हैं, आपको खुद को यह जानना चाहिए कि यह वास्तव में क्या बैक अप ले रहा है। – Peter

+0

@ पीटर क्या आप मुझे मेरे पोस्ट – agiles

+0

से संबंधित कुछ ट्यूटोरियल यूआरएल दे सकते हैं http://www.google.com/search?q=android+api+file+system – Peter

उत्तर

7
public ArrayList<String> smsBuffer = new ArrayList<String>(); 
    String smsFile = "SMS"+".csv"; 
     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 + smsFile; 
     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(); 
    } 

} 
+0

लेकिन सीएसवी फ़ाइल से एसएमएस कैसे पुनर्स्थापित करें ? – Azahar

+0

मैं अपने एसडी कार्ड में बैकअप sms.cvs फ़ाइल कैसे देख सकता हूं। मुझे उपरोक्त कोड निष्पादित करने के बाद मेरे एसडी कार्ड में कोई सीएसवी फ़ाइल नहीं मिल रही है। बैकअप फ़ाइल खोजने के लिए कृपया मेरी मदद करें। –

1
package com.vcard; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.View; 

public class VCardActivity extends Activity 
{ 
Cursor cursor; 
ArrayList<String> vCard ; 
String vfile; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf"; 
    /**This Function For Vcard And here i take one Array List in Which i store every Vcard String of Every Conatact 
    * Here i take one Cursor and this cursor is not null and its count>0 than i repeat one loop up to cursor.getcount() means Up to number of phone contacts. 
    * And in Every Loop i can make vcard string and store in Array list which i declared as a Global. 
    * And in Every Loop i move cursor next and print log in logcat. 
    * */ 
    getVcardString(); 

} 
private void getVcardString() { 
    // TODO Auto-generated method stub 
    vCard = new ArrayList<String>(); 
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
    if(cursor!=null&&cursor.getCount()>0) 
    { 
     cursor.moveToFirst(); 
     for(int i =0;i<cursor.getCount();i++) 
     { 

      get(cursor); 
      Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i)); 
      cursor.moveToNext(); 
     } 

    } 
    else 
    { 
     Log.d("TAG", "No Contacts in Your Phone"); 
    } 

} 

public void get(Cursor cursor) 
{ 


    //cursor.moveToFirst(); 
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
    AssetFileDescriptor fd; 
    try { 
     fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); 

     // Your Complex Code and you used function without loop so how can you get all Contacts Vcard.?? 


     /* FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String VCard = new String(buf); 
     String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; 
     FileOutputStream out = new FileOutputStream(path); 
     out.write(VCard.toString().getBytes()); 
     Log.d("Vcard", VCard);*/ 

     FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String vcardstring= new String(buf); 
     vCard.add(vcardstring); 

     String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; 
     FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false); 
     mFileOutputStream.write(vcardstring.toString().getBytes()); 

    } catch (Exception e1) 
    { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 
} 

इस में

धन्यवाद आप वीसीएफ फ़ाइल के रूप में संपर्क लेने के लिए मदद की जा सकती है। एसएमएस बैकअप लेने के लिए एसएमएस विधि बैकअप के लिए इस विधि का उपयोग करें।

+0

आपके उत्तर के लिए धन्यवाद – agiles

संबंधित मुद्दे