I want to set custom ring while receiving sms from user like "Demo" .Is it possible to set custom ring for single user ? I am trying this but can not achieve my requirement
final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, "9801205808");
final String[] projection = new String[]{
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
return;
}
final String storage = Environment.getExternalStorageDirectory().getPath();
final File file = new File(storage + "/AudioRecorder", "hello.mp4");
final String value = Uri.fromFile(file).toString();
final ContentValues values = new ContentValues(1);
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
getContentResolver().update(contactUri, values, null, null);
} finally {
data.close();
}
This is what I used to set Ringtone for a contact:
Cursor c =...; // query contact cursor
int dataIndex = c.getColumnIndexOrThrow(Contacts._ID);
String contactId = c.getString(dataIndex);
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, contactId);
ContentValues values = new ContentValues();
values.put(Contacts.CUSTOM_RINGTONE, mRingtoneUri.toString());
getContentResolver().update(uri, values, null, null);
Related
I am writing code for setting custom ringtone in android. Took help from other answers in stackoverflow. Here is the code.
private void setContactRingtone(String number) {
final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[]{
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
// Build your Cursor
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
// Get the contact lookup Uri
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
// Invalid arguments
return;
}
File file = new File(this.getFilesDir() + File.separator + "ringtone.mp3");
final String value = Uri.fromFile(file).toString();
final ContentValues values = new ContentValues(1);
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
getContentResolver().update(contactUri, values, null, null);
Toast.makeText(getApplicationContext(), "Done!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// Don't forget to close your Cursor
data.close();
}
}
The code is not assigning the user defined ringtone to the contact. There are no exceptions generated.
SO the problem was in android 10 asset folder is accessible through inputstream.Referred this answer
Here we create a temporary file and write to external storage. And then read that file.
I am creating an audio player and i am using this code to get all the songs
String[] STAR = {"*"};
Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
File file;
cursor =context.getContentResolver().query(uri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int i = 0;
do {
String songName = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
path[i] = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
albumId[i] = cursor
.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String albumname= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
artistId[i]=cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
}cursor.close();
This gets me the songs but i get audio of unsupported formats too. such as .wav any idea how to avoid unsupported formats
You can use MimeType to do this. See below example code how it is done for mp3.
ContentResolver contentResolver = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String sortOrder = null;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = imeTypeMap.getSingleton().getMimeTypeFromExtension("mp3");
String[] selectionArgsMp3 = new String[]{ mimeType };
Cursor mp3Songs = contentResolver.query(uri, projection,selectionMimeType,selectionArgsmp3, sortOrder);
Another approach it that you can check is there any compatible application installed on device using below code while opening/playing application.
private void openRelevantFileHandler(String pathToMusicFile)
{
String extension = MimeTypeMap.getFileExtensionFromUrl(pathToMusicFile);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + pathToMusicFile), type);
PackageManager packageManager = context.getPackageManager();
List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL);
boolean isIntentSafe = activities.size() > 0;
if (!isIntentSafe) {
Utility.showToast(context, "No application available on your device to open this type of file.");
}
else
{
context.startActivity(intent);//Start related application
}
}
Hope this will help you in some way :)
I have video path. I am able to load video in video using this path.
Now i want some video info from db but my Cursor is always null.
Below is my code.
String videoPath=Uri.fromFile(new File("/storage/emulated/0/Android/data/files/1483767006415.mp4")
final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DURATION};
CursorLoader loader = new CursorLoader(getActivity(), contentUri, projection, null, null, null);
Cursor cursor = loader.loadInBackground();
if (cursor != null && cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndex(projection[0]));
String name = cursor.getString(cursor.getColumnIndex(projection[1]));
String path = cursor.getString(cursor.getColumnIndex(projection[2]));
long duration = cursor.getLong(cursor.getColumnIndex(projection[3]));
cursor.close();
return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration);
}
Seems that uri is not proper.
Thanks.
Finally got answer.
Issue was in my URI. Video id was not appended in URI. So i have managed to get video id first and then createdURI. After using this URI, I was able to get all info. Below is code for same.
Uri mainUri;
Cursor cursor1 = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID},
MediaStore.Video.Media.DATA + "=? ",
new String[]{pathMain}, null);
if (cursor1 != null && cursor1.moveToFirst()) {
int id = cursor1.getInt(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
cursor1.close();
mainUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, pathMain);
mainUri = getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DURATION};
String selection = MediaStore.Video.Media.DATA + "=?";
String selectionArgs[] = {pathMain};
CursorLoader loader = new CursorLoader(getActivity(), mainUri, projection, selection, selectionArgs, null);
Cursor cursor = loader.loadInBackground();
if (cursor != null && cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndex(projection[0]));
String name = cursor.getString(cursor.getColumnIndex(projection[1]));
String path = cursor.getString(cursor.getColumnIndex(projection[2]));
long duration = cursor.getLong(cursor.getColumnIndex(projection[3]));
cursor.close();
return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration);
}
Given a contact name, I need to change its ringtone to the default ringtone,
Presently what I am doing is
1) Iterate through all the contacts
2) If the desired contact is found, prepare a contentvalues with the new ringtone(xperia.mp3 which is present in the Internal Storage.) and update it.
But the code is showing no effect ?
I am new to android and I need help in doing this, also read other answers, but they were not of great help.
String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
// Get cursor to all the names
Cursor nameCur = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
// if name equals name to check
if(display.equals(tocheck)){ // to check contains the name to check.
Uri contactData = ContactsContract.Contacts.CONTENT_URI;
String contactId = nameCur.getString(nameCur.getColumnIndexOrThrow("_id"));
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
Cursor localCursor = getActivity().managedQuery(contactData, PROJECTION, null, null, null);
localCursor.move(Integer.valueOf(contactId)/*CONTACT ID NUMBER*/);
String str1 = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
String str2 = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
ContentValues localContentValues = new ContentValues();
File newSoundFile = new File(Environment.getExternalStorageDirectory() + File.separator + "xperia.mp3"); // save new ringtone.
localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE,newSoundFile.getAbsolutePath());
getActivity().getContentResolver().update(localUri, localContentValues, null, null);
nameCur.close();
break;
}
}
I need to call recent call list and favourite call list on the click of respective buttons and need the data in my own list layout.I am new to android and having lot of trouble with this.can anyone please help me..thanks in advance..
With some extra, useful code:
getFavoriteContacts:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = getContentResolver().query(queryUri, projection, selection,null,null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
getRecentContacts:
Map getRecentContacts(){
Map contactMap = new HashMap();
Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.DATE};
String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");
Cursor cursor = getContentResolver().query(queryUri, projection, null,null,sortOrder);
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
if(phoneNumber==null||title==null)continue;
String uri = "tel:" + phoneNumber ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
String intentUriString = intent.toUri(0);
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}
For getting recent calls list,you can use CallLog in android. Here is a good tutorial.This is also helpful.
You can use it for all outgoing calls like this :
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null);
For all types of calls,use it like:
Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null);
You can use the following Java code to get the recent calls list:
Cursor phones = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(CallLog.Calls.NUMBER));
if (phoneNumber.length() > 6) {
String name = phones.getString(phones.getColumnIndex(CallLog.Calls.CACHED_NAME));
numbers.add(phoneNumber);
}
}