how to delete a google calendar event? - java

I write an android app that fetches my events,
How can I identify an event (some unique ID)
and delete it or edit it?
private void fetchEvents() {
String[] selection = new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" };
String projection = "description LIKE ?";
String[] selecionArgs = new String[]{"%/images/%"};
String orderby = "dtstart ASC";
Cursor cursor = getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
selection, projection,
selecionArgs, orderby);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(new Date(Long.parseLong(cursor.getString(3))));
//endDates.add(new Date(Long.parseLong(cursor.getString(4))));
String fullDescription = cursor.getString(2);
int firstIndex = fullDescription.indexOf("localUri: ");
firstIndex = (firstIndex < 0)? 0 : 10;
int lastIndex = fullDescription.indexOf("\n");
lastIndex = (lastIndex < 0)? fullDescription.length() : lastIndex;
descriptions.add(fullDescription.substring(firstIndex, lastIndex));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
}

Related

Scoped Storage android 10.0

Crash is here......
android.database.sqlite.SQLiteException: near "GROUP": syntax error (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT _id, bucket_display_name, bucket_id, _id, orientation FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ((1) GROUP BY 1,(2)) ORDER BY date_modified DESC, (OS error - 2:No such file or directory)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
at android.content.ContentResolver.query(ContentResolver.java:955)
at android.content.ContentResolver.query(ContentResolver.java:891)
at android.content.ContentResolver.query(ContentResolver.java:840)
Below is method to show all folders or album of images.
private boolean logGalleryFolders() {
this.albumList = new ArrayList<>();
List<Integer> bucketIdList = new ArrayList<>();
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
List<GridViewItem> items;
int i;
if (cur == null || !cur.moveToFirst()) {
items = new ArrayList<>();
for (i = 0; i < this.albumList.size(); i++) {
items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
}
this.albumList.add(new Album());
this.albumList.get(this.albumList.size() - 1).gridItems = items;
for (i = 0; i < this.albumList.size() - 1; i++) {
this.albumList.get(i).gridItems = createGridItemsOnClick(i);
}
if (cur != null) {
cur.close();
}
return true;
}
int bucketColumn = cur.getColumnIndex("bucket_display_name");
int bucketId = cur.getColumnIndex("bucket_id");
int imageId = cur.getColumnIndex("_id");
int orientationColumnIndex = cur.getColumnIndex("orientation");
do {
Album album = new Album();
int id = cur.getInt(bucketId);
album.ID = id;
if (bucketIdList.contains(id)) {
Album albumFromList = this.albumList.get(bucketIdList.indexOf(album.ID));
albumFromList.imageIdList.add(cur.getLong(imageId));
albumFromList.orientationList.add(cur.getInt(orientationColumnIndex));
} else {
String bucket = cur.getString(bucketColumn);
bucketIdList.add(id);
album.name = bucket;
album.imageIdForThumb = cur.getLong(imageId);
album.imageIdList.add(album.imageIdForThumb);
this.albumList.add(album);
album.orientationList.add(cur.getInt(orientationColumnIndex));
}
} while (cur.moveToNext());
items = new ArrayList<>();
for (i = 0; i < this.albumList.size(); i++) {
items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
}
this.albumList.add(new Album());
this.albumList.get(this.albumList.size() - 1).gridItems = items;
for (i = 0; i < this.albumList.size() - 1; i++) {
this.albumList.get(i).gridItems = createGridItemsOnClick(i);
}
cur.close();
return true;
}
Below is crash line......
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
Just change the query like below:
From
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
To
Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, null, null, "date_modified DESC");
Your GROUP BY comes inside the And makes the issue. Please change your query as below:
SELECT
_id,
bucket_display_name,
bucket_id,
_id,
orientation
FROM
images
WHERE
(
(is_pending = 0)
AND (is_trashed = 0)
AND (volume_name IN ('external_primary'))
)
AND ((1))
GROUP BY
1,(2)
ORDER BY
date_modified DESC

Android - How to retrieve contacts list as in native app?

I'm looking to export the contacts that are stored in the native database.
I'm having trouble retrieving contacts from native database.
Here is the query I would like to use :
Get all the contacts that have at least a phone number or an email.
Here is the query I am using :
String dataWhere = ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?";
String[] dataWhereValues = new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String[] dataProjection = new String[]{ContactsContract.Data.CONTACT_ID, ContactsContract.Data.LOOKUP_KEY, ContactsContract.Data.DISPLAY_NAME_PRIMARY, ContactsContract.Data.STARRED, ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1, ContactsContract.Data.DATA2, ContactsContract.Data.DATA_VERSION};
Cursor data = getContentResolver().query(ContactsContract.Data.CONTENT_URI, dataProjection, dataWhere, dataWhereValues, ContactsContract.Data.CONTACT_ID);
But this query gives me lots of weird contacts, and some of my contacts are also missing...
Can anyone help me please ?
this may help you for getting phone numbers and display names...
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// importing phone contacts
while (phones.moveToNext())
{
listMobileNo.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
listName.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
}
phones.close();
Here is the query I ended up with
String where = ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?";
String[] whereValues = new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] dataProjection = new String[]{ContactsContract.Data.CONTACT_ID, ContactsContract.Data.LOOKUP_KEY, ContactsContract.Data.DISPLAY_NAME_PRIMARY, ContactsContract.Data.STARRED, ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1, ContactsContract.Data.DATA2, ContactsContract.Data.DATA_VERSION};
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, dataProjection, dataWhere, dataWhereValues, ContactsContract.Data.CONTACT_ID + " ASC");
int cidIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.CONTACT_ID);
int nameIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME_PRIMARY);
int starredIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.STARRED);
int typeIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE);
int data1Index = cursor.getColumnIndexOrThrow(ContactsContract.Data.DATA1);
int data2Index = cursor.getColumnIndexOrThrow(ContactsContract.Data.DATA2);
int lookupKeyIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.LOOKUP_KEY);
int versionIndex = cursor.getColumnIndexOrThrow(ContactsContract.Data.DATA_VERSION);
boolean hasData = cursor.moveToNext();
while (hasData){
contactId = cursor.getLong(cidIndex);
nativeLookupKey = cursor.getString(lookupKeyIndex);
fullName = cursor.getString(nameIndex);
starred = cursor.getInt(starredIndex);
version = cursor.getLong(versionIndex);
while (currentContactId <= contactId && hasData) {
if (currentContactId == contactId) {
String type = cursor.getString(typeIndex);
if (type.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
String email = cursor.getString(data1Index);
} else if (type.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
String number = cursor.getString(data1Index);
}
}
hasData = cursor.moveToNext();
if (hasData) {
currentContactId = cursor.getLong(cidIndex);
}
}
}

insert song to playlist returns null

I was trying to build a music player application , and I wanted to add a feature in order to add a song to a playlist.Here is the code:
public static void addToPlaylist(ContentResolver resolver,SongDetails songDetails,int playlistId) {
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + songDetails.getId()));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songDetails.getId());
values.put(MediaStore.Audio.Playlists.Members.DISPLAY_NAME, songDetails.getSongTitle());
values.put(MediaStore.Audio.Playlists.Members.ARTIST, songDetails.getArtistName());
values.put(MediaStore.Audio.Playlists.Members.DATA, songDetails.getSongData());
Log.i("URI:",resolver.insert(uri, values)+"");
}
and in the logs I always get:
URI: null
which means that the song is not added to the playlist.Does anybody have any idea why the song is not added?
I've been using this method and it worked for me.
public static String addTracksToPlaylist(final long id, List<MediaData> tracks, final Context context) {
int count = getPlaylistSize(id, context);
ContentValues[] values = new ContentValues[tracks.size()];
for (int i = 0; i < tracks.size(); i++) {
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i + count + 1);
values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, tracks.get(i).getId());
}
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", id);
ContentResolver resolver = context.getContentResolver();
int num = resolver.bulkInsert(uri, values);
resolver.notifyChange(Uri.parse("content://media"), null);
return String.format(context.getString(R.string.ADDED_TO_PLAYLIST), num, context.getString(R.string.CURRENT));
}
Retrieving tracks from playlist:
public static Cursor getTrackListFromPlaylist(Context context, long plid) {
String[] MEDIA_COLUMNS = new String[] {
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MINUTES + "||':'||" + SECONDS + " as " + FORMATTED_DURATION,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media._ID
};
return query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
MEDIA_COLUMNS, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
}

Get music-files of one directory

In our app we want to get the music-files of one specific folder. We want to do this, to use the media store.
So we have a path /mnt/sd/music of this dir we will need all the files and not of the subdirs.
We tried to make a query with a substring, but it doesnt work.
public ArrayList<Song> getAudioFilesOfDir(String pathDirectory)
{
ArrayList<Song> songs = new ArrayList<Song>();
//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + android.provider.MediaStore.Audio.Media.DATA.substring(pathDirectory.length()).contains("/") ;
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_KEY,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.TITLE_KEY,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.ARTIST
};
ContentResolver cr = CoreLib.Context().getContentResolver();
Cursor cursor = cr.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
MediaStore.Audio.Media._ID);
int _ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
int DATA_Column = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int TRACK_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int YEAR_Column = cursor.getColumnIndex(MediaStore.Audio.Media.YEAR);
int DURATION_Column = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
int ALBUM_ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
int ALBUM_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
//int ALBUM_KEY_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY);
//int TITLE_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
//int TITLE_KEY_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE_KEY);
//int ARTIST_ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID);
int ARTIST_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
while(cursor.moveToNext())
{
Song song = new Song();
song.setTrackId(cursor.getInt(_ID_Column));
song.setPathAudioFile(cursor.getString(DATA_Column));
song.setTrackName(cursor.getString(TRACK_Column));
song.setYearTrack(cursor.getInt(YEAR_Column));
song.setDuration(cursor.getString(DURATION_Column));
song.setAlbumId(cursor.getInt(ALBUM_ID_Column));
song.setAlbumName(cursor.getString(ALBUM_Column));
song.setArtist(cursor.getString(ARTIST_Column));
songs.add(song);
}
return songs;
}
I can tell that this won't work:
android.provider.MediaStore.Audio.Media.DATA.substring(pathDirectory.length()).contains("/")
MediaStore.Audio.Media.DATA just contains a constant for database column and operating on that is just wrong.
You might find your solution in these answers.

How to get the contact group id or name?

I can't get the group name under which the contact is stored. I can get whether it is added in any group as boolean value( IN_VISIBLE_GROUP).I have no idea how to get the group name or id.
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String group = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.IN_VISIBLE_GROUP));
i have tried using ContactsContract.Groups and ContactsContract.Groups and ContactsContract.CommonDataKinds.GroupMembership but that is not the solution.
This is a full version of how to get the group id by its title. Basically it iterates all groups and compare titles to find its id.
private String getGroupId(String groupTitle) {
Cursor cursor =getContentResolver().query(ContactsContract.Groups.CONTENT_URI,new String[]{ContactsContract.Groups._ID,ContactsContract.Groups.TITLE}, null, null, null);
cursor.moveToFirst();
int len = cursor.getCount();
String groupId = null;
for (int i = 0; i < len; i++) {
String id = cursor.getString(cursor.getColumnIndex(Groups._ID));
String title = cursor.getString(cursor.getColumnIndex(Groups.TITLE));
if (title.equals(groupTitle)) {
groupId = id;
break;
}
cursor.moveToNext();
}
cursor.close();
return groupId;
}
Please change
ContentResolver cr = this.getContentResolver();
to
Uri groupURI = ContactsContract.Data.CONTENT_URI;
Cursor cur = cr.query(groupURI , null, null, null, null);
// Now you can get group ID from cursor
String groupRowId = cur.getString (cur.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));
public Integer getGroupId(String groupTitle) {
String[] PROJECTION = {ContactsContract.Groups._ID};
String SELECTION = ContactsContract.Groups.TITLE + "=?";
try (Cursor cursor = mContentResolver.query(ContactsContract.Groups.CONTENT_URI,
PROJECTION, SELECTION, new String[]{groupTitle}, null)) {
if (cursor.getCount()>0){
cursor.moveToFirst();
Integer id= cursor.getInt(0);
return id;
}else
return 0;
}
}

Categories