Advertisement
saurabh26213

Untitled

Jul 29th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. as you provide me the code i make two class and run it but it fc..my error is also give below..pls take a look..
  2.  
  3. CsvSender class is
  4.  
  5.  
  6. package contactlist.pkg;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12.  
  13. import android.app.Activity;
  14. import android.content.Intent;
  15. import android.database.Cursor;
  16. import android.net.Uri;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.provider.ContactsContract;
  20. import android.provider.ContactsContract.CommonDataKinds.Phone;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.Toast;
  25.  
  26. public class CsvSender extends Activity {
  27.  
  28. private boolean csv_status = false;
  29.  
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34.  
  35. createCSV();
  36. if(csv_status)
  37. exportCSV();
  38. }
  39. private void createCSV() {
  40. CSVWriter writer = null;
  41. try {
  42. writer = new CSVWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my_test_contact.csv"));
  43. } catch (IOException e1) {
  44. // TODO Auto-generated catch block
  45. e1.printStackTrace();
  46. }
  47. String displayName;
  48. String number;
  49. long _id;
  50. String columns[] = new String[]{ ContactsContract.Contacts._ID,
  51. ContactsContract.Contacts.DISPLAY_NAME };
  52. writer.writeColumnNames(); // Write column header
  53. Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
  54. columns,
  55. null,
  56. null,
  57. ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
  58. startManagingCursor(cursor);
  59. if(cursor.moveToFirst()) {
  60. do {
  61. _id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
  62. displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();
  63. number = getPrimaryNumber(_id);
  64. writer.writeNext((displayName + "/" + number).split("/"));
  65. } while(cursor.moveToNext());
  66. csv_status = true;
  67. } else {
  68. csv_status = false;
  69. }
  70. try {
  71. if(writer != null)
  72. writer.close();
  73. } catch (IOException e) {
  74. Log.w("Test", e.toString());
  75. }
  76.  
  77. }// Method close.
  78.  
  79.  
  80. private void exportCSV() {
  81. if(csv_status == true) {
  82. //CSV file is created so we need to Export that ...
  83. final File CSVFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my_test_contact.csv");
  84. //Log.i("SEND EMAIL TESTING", "Email sending");
  85. Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
  86. emailIntent.setType("text/csv");
  87. emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "Test contacts ");
  88. emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, "\n\nAdroid developer\n Pankaj");
  89. emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + CSVFile.getAbsolutePath()));
  90. emailIntent.setType("message/rfc822"); // Shows all application that supports SEND activity
  91. try {
  92. startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  93. } catch (android.content.ActivityNotFoundException ex) {
  94. Toast.makeText(getApplicationContext(), "Email client : " + ex.toString(), Toast.LENGTH_SHORT);
  95. }
  96. } else {
  97. Toast.makeText(getApplicationContext(), "Information not available to create CSV.", Toast.LENGTH_SHORT).show();
  98. }
  99. }
  100. /**
  101. * Get primary Number of requested id.
  102. *
  103. * @return string value of primary number.
  104. */
  105. private String getPrimaryNumber(long _id) {
  106. String primaryNumber = null;
  107. try {
  108. Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  109. new String[]{Phone.NUMBER, Phone.TYPE},
  110. ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ _id, // We need to add more selection for phone type
  111. null,
  112. null);
  113. if(cursor != null || cursor.getCount() > 0) {
  114. while(cursor.moveToNext()){
  115. switch(cursor.getInt(cursor.getColumnIndex(Phone.TYPE))){
  116. case Phone.TYPE_MOBILE :
  117. primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
  118. break;
  119. case Phone.TYPE_HOME :
  120. primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
  121. break;
  122. case Phone.TYPE_WORK :
  123. primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
  124. break;
  125. case Phone.TYPE_OTHER :
  126. }
  127. if(primaryNumber != null)
  128. break;
  129. }
  130. }
  131.  
  132. if(!cursor.isClosed() || cursor != null) {
  133. cursor.deactivate();
  134. cursor.close();
  135. }
  136. } finally {
  137.  
  138.  
  139. }
  140. return primaryNumber;
  141. }
  142. }
  143.  
  144.  
  145. and CSVWriter class is
  146.  
  147.  
  148.  
  149.  
  150. package contactlist.pkg;
  151.  
  152. import java.io.IOException;
  153. import java.io.PrintWriter;
  154. import java.io.Writer;
  155.  
  156. public class CSVWriter {
  157.  
  158. private PrintWriter pw;
  159. private char separator;
  160. private char quotechar;
  161. private char escapechar;
  162. private String lineEnd;
  163.  
  164. /** The character used for escaping quotes. */
  165. public static final char DEFAULT_ESCAPE_CHARACTER = '"';
  166.  
  167. /** The default separator to use if none is supplied to the constructor. */
  168. public static final char DEFAULT_SEPARATOR = ',';
  169.  
  170. /**
  171. * The default quote character to use if none is supplied to the
  172. * constructor.
  173. */
  174. public static final char DEFAULT_QUOTE_CHARACTER = '"';
  175.  
  176. /** The quote constant to use when you wish to suppress all quoting. */
  177. public static final char NO_QUOTE_CHARACTER = '\u0000';
  178.  
  179. /** The escape constant to use when you wish to suppress all escaping. */
  180. public static final char NO_ESCAPE_CHARACTER = '\u0000';
  181.  
  182. /** Default line terminator uses platform encoding. */
  183. public static final String DEFAULT_LINE_END = "\n";
  184.  
  185. /** Default column name. */
  186. public static final String DEFAULT_COLUMN_NAME = "Contact Name,Phone Number,";
  187.  
  188. /**
  189. * Constructs CSVWriter using a comma for the separator.
  190. *
  191. * @param writer
  192. * the writer to an underlying CSV source.
  193. */
  194. public CSVWriter(Writer writer) {
  195. this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,
  196. DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
  197. }
  198.  
  199. /**
  200. * Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
  201. *
  202. * @param writer
  203. * the writer to an underlying CSV source.
  204. * @param separator
  205. * the delimiter to use for separating entries
  206. * @param quotechar
  207. * the character to use for quoted elements
  208. * @param escapechar
  209. * the character to use for escaping quotechars or escapechars
  210. * @param lineEnd
  211. * the line feed terminator to use
  212. */
  213. public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
  214. this.pw = new PrintWriter(writer);
  215. this.separator = separator;
  216. this.quotechar = quotechar;
  217. this.escapechar = escapechar;
  218. this.lineEnd = lineEnd;
  219. }
  220.  
  221. /**
  222. * Writes the next line to the file.
  223. *
  224. * @param nextLine
  225. * a string array with each comma-separated element as a separate
  226. * entry.
  227. */
  228. public void writeNext(String[] nextLine) {
  229.  
  230. if (nextLine == null)
  231. return;
  232.  
  233. StringBuffer sb = new StringBuffer();
  234. for (int i = 0; i < nextLine.length; i++) {
  235.  
  236. if (i != 0) {
  237. sb.append(separator);
  238. }
  239.  
  240. String nextElement = nextLine[i];
  241. if (nextElement == null)
  242. continue;
  243. if (quotechar != NO_QUOTE_CHARACTER)
  244. sb.append(quotechar);
  245. for (int j = 0; j < nextElement.length(); j++) {
  246. char nextChar = nextElement.charAt(j);
  247. if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
  248. sb.append(escapechar).append(nextChar);
  249. } else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
  250. sb.append(escapechar).append(nextChar);
  251. } else {
  252. sb.append(nextChar);
  253. }
  254. }
  255. if (quotechar != NO_QUOTE_CHARACTER)
  256. sb.append(quotechar);
  257. }
  258.  
  259. sb.append(lineEnd);
  260. pw.write(sb.toString());
  261.  
  262. }
  263.  
  264. public void writeColumnNames() {
  265. writeNext(DEFAULT_COLUMN_NAME.split(","));
  266. }
  267.  
  268. /**
  269. * Flush underlying stream to writer.
  270. *
  271. * @throws IOException if bad things happen
  272. */
  273. public void flush() throws IOException {
  274. pw.flush();
  275. }
  276.  
  277. /**
  278. * Close the underlying stream writer flushing any buffered content.
  279. *
  280. * @throws IOException if bad things happen
  281. *
  282. */
  283. public void close() throws IOException {
  284. pw.flush();
  285. pw.close();
  286. }
  287.  
  288. }
  289.  
  290.  
  291.  
  292.  
  293.  
  294. 07-29 15:35:51.742: ERROR/AndroidRuntime(8062): Caused by: java.lang.NullPointerException
  295. 07-29 15:35:51.742: ERROR/AndroidRuntime(8062): at contactlist.pkg.CsvSender.createCSV(CsvSender.java:47)
  296. 07-29 15:35:51.742: ERROR/AndroidRuntime(8062): at contactlist.pkg.CsvSender.onCreate(CsvSender.java:30)
  297. 07-29 15:35:51.742: ERROR/AndroidRuntime(8062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  298. 07-29 15:35:51.742: ERROR/AndroidRuntime(8062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement