List of usage examples for android.content ContentResolver query
public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)
From source file:me.ububble.speakall.fragment.ConversationGroupFragment.java
public void showDialogAddContact(final String contacto) { final boolean[] agregar = { false }; try {/*from w ww . j a v a2s . c o m*/ final ContentResolver contentResolver = activity.getContentResolver(); final JSONObject contact = new JSONObject(contacto); String name = null; String phone = null; String selectionArgs = ContactsContract.Contacts.DISPLAY_NAME + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "= " + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; Cursor c = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, selectionArgs, new String[] { contact.getString("nombre") }, null); if (c.getCount() > 0) { if (c.moveToFirst()) { phone = c.getString(0); } if (phone.equals(contact.getString("telefono"))) { Toast.makeText(activity, "Ya tienes este contacto", Toast.LENGTH_SHORT).show(); } else { LayoutInflater dialogInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); dialogView = dialogInflater.inflate(R.layout.add_contact_message, null); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setView(dialogView) .setCancelable(true).setPositiveButton(R.string.add_contact_yes, null) .setNegativeButton(R.string.add_contact_no, null); finderDialog = dialogBuilder.show(); finderDialog.setCanceledOnTouchOutside(true); finderDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { finderDialog.dismiss(); agregar[0] = true; if (agregar[0]) { ContentValues values = new ContentValues(); values.put(ContactsContract.Data.DISPLAY_NAME, contact.getString("nombre")); Uri rawContactUri = contentResolver .insert(ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); long contactId = ContentUris.parseId(rawContactUri); values.clear(); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getString("telefono")); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); } } catch (JSONException e) { e.printStackTrace(); } } }); finderDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finderDialog.dismiss(); agregar[0] = false; } }); } } else { LayoutInflater dialogInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); dialogView = dialogInflater.inflate(R.layout.add_contact_message, null); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setView(dialogView) .setCancelable(true).setPositiveButton(R.string.add_contact_yes, null) .setNegativeButton(R.string.add_contact_no, null); finderDialog = dialogBuilder.show(); finderDialog.setCanceledOnTouchOutside(true); finderDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { finderDialog.dismiss(); agregar[0] = true; if (agregar[0]) { ContentValues values = new ContentValues(); values.put(ContactsContract.Data.DISPLAY_NAME, contact.getString("nombre")); Uri rawContactUri = contentResolver .insert(ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); values.clear(); values.put(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getString("telefono")); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.Data.CONTENT_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getString("nombre")); values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getString("nombre")); values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); } } catch (JSONException e) { e.printStackTrace(); } } }); finderDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finderDialog.dismiss(); agregar[0] = false; } }); } c.close(); } catch (JSONException e) { e.printStackTrace(); } }
From source file:me.ububble.speakall.fragment.ConversationChatFragment.java
public void showDialogAddContact(final String contacto) { final boolean[] agregar = { false }; try {// w ww .j a va2 s. c om final ContentResolver contentResolver = activity.getContentResolver(); final JSONObject contact = new JSONObject(contacto); String name = null; String phone = null; String selectionArgs = ContactsContract.Contacts.DISPLAY_NAME + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "= " + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; Cursor c = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, selectionArgs, new String[] { contact.getString("nombre") }, null); if (c.getCount() > 0) { if (c.moveToFirst()) { phone = c.getString(0); } if (phone.equals(contact.getString("telefono"))) { Toast.makeText(activity, "Ya tienes este contacto", Toast.LENGTH_SHORT).show(); } else { LayoutInflater dialogInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); dialogView = dialogInflater.inflate(R.layout.add_contact_message, null); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setView(dialogView) .setCancelable(true).setPositiveButton(R.string.add_contact_yes, null) .setNegativeButton(R.string.add_contact_no, null); finderDialog = dialogBuilder.show(); finderDialog.setCanceledOnTouchOutside(true); finderDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { finderDialog.dismiss(); agregar[0] = true; if (agregar[0]) { ContentValues values = new ContentValues(); values.put(ContactsContract.Data.DISPLAY_NAME, contact.getString("nombre")); Uri rawContactUri = contentResolver .insert(ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); long contactId = ContentUris.parseId(rawContactUri); values.clear(); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getString("telefono")); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); values.put(Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); } } catch (JSONException e) { e.printStackTrace(); } } }); finderDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finderDialog.dismiss(); agregar[0] = false; } }); } } else { LayoutInflater dialogInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); dialogView = dialogInflater.inflate(R.layout.add_contact_message, null); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setView(dialogView) .setCancelable(true).setPositiveButton(R.string.add_contact_yes, null) .setNegativeButton(R.string.add_contact_no, null); finderDialog = dialogBuilder.show(); finderDialog.setCanceledOnTouchOutside(true); finderDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { finderDialog.dismiss(); agregar[0] = true; if (agregar[0]) { ContentValues values = new ContentValues(); values.put(ContactsContract.Data.DISPLAY_NAME, contact.getString("nombre")); Uri rawContactUri = contentResolver .insert(ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); values.clear(); values.put(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getString("telefono")); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); values.put(Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(Data.MIMETYPE, ContactsContract.Data.CONTENT_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getString("nombre")); values.put(Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getString("nombre")); values.put(Data.RAW_CONTACT_ID, rawContactId); contentResolver.insert(ContactsContract.Data.CONTENT_URI, values); } } catch (JSONException e) { e.printStackTrace(); } } }); finderDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finderDialog.dismiss(); agregar[0] = false; } }); } c.close(); } catch (JSONException e) { e.printStackTrace(); } }
From source file:com.tct.mail.compose.ComposeActivity.java
@SuppressLint("NewApi") public String getFilePath(Uri uri) { Uri thisUri = null;//from ww w . ja v a2s. co m String path = ""; ContentResolver cr = this.getContentResolver(); if (uri == null) { return path; } thisUri = uri; try { String scheme = thisUri.getScheme(); if (scheme == null) { path = thisUri.toString(); } else if (scheme.equals("file")) { path = thisUri.getPath(); path = changeDrmFileSuffix(path); } else if (DocumentsContract.isDocumentUri(this, uri)) { //ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(this, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } contentUri = contentUri.withAppendedPath(contentUri, split[1]); return getDataColumn(this, contentUri, null, null); } } else if (scheme.equals("content")) { String[] projection = { "_data" }; Cursor c = cr.query(thisUri, projection, null, null, null); if (c != null) { try { if (c.moveToFirst()) { path = c.getString(0); } } finally { c.close(); } } if (path.endsWith("RAW")) { List<String> segments = thisUri.getPathSegments(); String dbName = segments.get(0); String id = segments.get(1); path = this.getDatabasePath(dbName + "_att") + "/" + id; } } } catch (Exception e) { } return path; }
From source file:com.codename1.impl.android.AndroidImplementation.java
private String getContentName(ContentResolver resolver, Uri uri) { Cursor cursor = resolver.query(uri, null, null, null, null); cursor.moveToFirst();//from w w w . j a va2s . c om int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME); if (nameIndex >= 0) { String name = cursor.getString(nameIndex); cursor.close(); return name; } return null; }