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.
Related
I am able to read the file i created with mediastore in android 10
But I can't read files created by other apps
'java.io.IOException: setDataSource failed.: status=0x80000000' error occurs when trying to access mediaPlayer from other apps
How can I read files created by other apps?
Below is the code to get the list of files in the MUSIC folder
public void getFileListInFolderApiQ(String path) {
mDataSet.clear();
Uri externalUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.MediaColumns.RELATIVE_PATH + "=?";
String[] selectionArgs = new String[]{Environment.DIRECTORY_MUSIC + "/" + "tts" + "/"};
String[] projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.MIME_TYPE
};
Cursor cursor = getContentResolver().query(externalUri, projection, selection, selectionArgs, null);
Uri testUri = null;
if (cursor == null || !cursor.moveToFirst()) {
return;
}
do {
String contentUrl = externalUri.toString() + "/" + cursor.getString(0);
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri uri = ContentUris.withAppendedId(externalUri, id);
String tmpFilename = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
testUri = uri;
mDataSet.add(new CustomTTSListData(tmpFilename, getPathFromUri(testUri), 1, 1, uri));
} while (cursor.moveToNext());
}
I want to delete a single occurrence of a repeated event. This is my code:
private void handleActionDelete(long event, long occurrence) {
final ContentResolver contentResolver = getContentResolver();
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, Long.MIN_VALUE);
ContentUris.appendId(eventsUriBuilder, Long.MAX_VALUE);
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor;
String[] projection;
projection = new String[]{CalendarContract.Instances.BEGIN};
String selection = "Instances." + CalendarContract.Instances._ID + " = ? AND " + CalendarContract.Instances
.EVENT_ID + " = ?";
String[] selArgs = new String[]{Long.toString(occurrence), Long.toString(event)};
cursor = getContentResolver().query(eventsUri, projection, selection, selArgs, null);
if (cursor == null) {
return;
}
if (cursor.getCount() == 0) {
cursor.close();
return;
}
cursor.moveToFirst();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, cursor.getLong(cursor.getColumnIndex
(CalendarContract.Instances.BEGIN)));
values.put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CANCELED);
Uri uri = Uri.withAppendedPath(CalendarContract.Events.CONTENT_EXCEPTION_URI, String.valueOf(event));
cursor.close();
try {
contentResolver.insert(uri, values);
} catch (Exception ignored) {
}
}
I already checked all other similar problems on stackoverflow but I didn't find any solution, maybe it's something related to my code, I hope someone can review it.
if you use com.google.api.services.calendar.Calendar I suppose you already have id of the event you want to delete
the answer is right there in the documentation and states (I've copied the exact snippet from doc):
// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName("applicationName").build();
// Delete an event
service.events().delete('primary', "eventId").execute();
I'm saving an image loaded with Picasso to be shared afterwards. This is the code I'm using to save it:
String path = MediaStore.Images.Media.insertImage(
mContext.getContentResolver(), mImageBitmap, "Shared image", null);
return Uri.parse(path);
Now, when the image has been shared, I have to delete it using the URI. I've tried some answers I've seen here but none of them has worked. The image still appears in the gallery. These are the methods I have used:
// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { new File(mImageUri.toString()).getAbsolutePath() };
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst())
{
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
}
c.close();
File file = new File(mImageUri.toString());
// This returns false
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mImageUri.toString()))));
Any idea of what i've done wrong?
Thanks,
Try this for deletion
String[] retCol = { MediaStore.Audio.Media._ID };
Cursor cur = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
retCol,
MediaStore.Images.Media.TITLE + "='"+TEMP_FILE_TITLE+"'", null, null
);
try {
if (cur.getCount() == 0) {
return;
}
cur.moveToFirst();
int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));
Uri uri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id
);
int cnt = context.getContentResolver().delete(uri, null, null);
}finally {
cur.close();
}
In your case replace TEMP_FILE_TITLE = "Shared image"
I know that I can retrieve the id of image using the following code:
String[] projection = { MediaStore.Images.Media._ID };
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { mediaFile.mediaFile().getAbsolutePath() };
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if(cursor!=null) {
if (cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
}
cursor.close();
}
I wonder, if there is any possible way to MediaStore.Images.Media.DATA_TAKEN, MediaStore.Images.Media.LONGITUDE, andMediaStore.Images.Media.LATITUDE using the same approach?
Provided you have the file path, you can get the date this way:
File file = new File(filePath);
if(file.exists()) //Extra check, Just to validate the given path
{
Date lastModDate = new Date(file.lastModified());
Log.i("Dated : "+ lastModDate.toString());//Dispaly lastModDate. You can do/use it your own way
}
An alternative option to find this would be from the EXIF data of the image, if its available:
ExifInterface intf = null;
try
{
intf = new ExifInterface(path);
}
catch(IOException e)
{
e.printStackTrace();
}
if(intf != null)
{
String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
Log.i("Dated : "+ dateString.toString()); //Dispaly dateString. You can do/use it your own way
}
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);