Content Provider - How to get data from Content Provider?

It is a shared set application data, stored on a steady and persistent storage like file, web, Database etc. Other application can query the application on request.

Here we will Query data from other application like message, gallery and contact of Android System.

Get SMS



void getSMS(Context context) {
  try {
      Uri uri = Uri.parse("content://sms");
      Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
      if (cursor.moveToFirst()) {
          for (int i = 0; i < cursor.getCount(); i++) {//c.getCount()
              String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
              String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
              String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
              String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString();
              int indexDate = cursor.getColumnIndex("date");
              long dates = cursor.getLong(indexDate);
              Date dateVal = new Date(dates);
              SimpleDateFormat format = new SimpleDateFormat("MM dd, yyyy HH:mm:ss");
              String dateText = format.format(dateVal);
              String typeOfSMS = null;
              if (type.equals("1"))
                  typeOfSMS = "INBOX";
              else if (type.equals("2"))
                  typeOfSMS = "SENT";
              else if (type.equals("3"))
                  typeOfSMS = "DRAFT";
              cursor.moveToNext();
          }
      }
      cursor.close();
  } catch (SecurityException e) {

  } catch (Exception e) {
  }
}


Get Contact



private void getContactList(Context context) {
  ContentResolver contentResolver = context.getContentResolver();
  Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
          null, null, null, null);
  if ((cursor != null ? cursor.getCount() : 0) > 0) {
      while (cursor != null && cursor.moveToNext()) {
          String id = cursor.getString(
                  cursor.getColumnIndex(ContactsContract.Contacts._ID));
          String name = cursor.getString(cursor.getColumnIndex(
                  ContactsContract.Contacts.DISPLAY_NAME));

          if (cursor.getInt(cursor.getColumnIndex(
                  ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
              Cursor pCur = contentResolver.query(
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                      null,
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                      new String[]{id}, null);
              while (pCur.moveToNext()) {
                  String phoneNo = pCur.getString(pCur.getColumnIndex(
                          ContactsContract.CommonDataKinds.Phone.NUMBER));
              }
              pCur.close();
          }
      }
  }
  if (cursor != null) {
      cursor.close();
  }
}


Get Call Log




private void getCallDetails(Context context) {
  Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
          null, null, null);
  int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
  int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
  int date = cursor.getColumnIndex(CallLog.Calls.DATE);
  int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
  while (cursor.moveToNext()) {
      String phNumber = cursor.getString(number);
      String callType = cursor.getString(type);
      String callDate = cursor.getString(date);
      Date callDayTime = new Date(Long.valueOf(callDate));
      String callDuration = cursor.getString(duration);
      String dir = null;
      int dircode = Integer.parseInt(callType);
      switch (dircode) {
          case CallLog.Calls.OUTGOING_TYPE:
              dir = "OUTGOING";
              break;

          case CallLog.Calls.INCOMING_TYPE:
              dir = "INCOMING";
              break;

          case CallLog.Calls.MISSED_TYPE:
              dir = "MISSED";
              break;
          default:
              dir = "UnTraceable";
              break;
      }
  }
  cursor.close();
}


Get Videos



private ArrayList<Uri> getVideoList(Context context) {
  ArrayList<Uri> fileList = new ArrayList<Uri>();
  try {
      String[] projection = {MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID};
      Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
              null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);

      int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);

      for (int i = 0; i < cursor.getCount(); i++) {
          cursor.moveToPosition(i);
          String fileName = cursor.getString(actual_image_column_index);
          fileList.add((Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, fileName)));
      }
      return fileList;
  } catch (Exception e) {
      return null;
  }
}


Get Images



private ArrayList<Uri> getImageList(Context context) {
  ArrayList<Uri> fileList = new ArrayList<Uri>();
  try {
      String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
      Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
              null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);

      int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

      for (int i = 0; i < cursor.getCount(); i++) {
          cursor.moveToPosition(i);
          String fileName = cursor.getString(actual_image_column_index);
          fileList.add((Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, fileName)));
      }
      return fileList;
  } catch (Exception e) {
      return null;
  }
}

Comments