I want to get the ringtones of a phone, but see only the english (non-localized version).
My theory, was using a ContextWrapper, and set that context locale to en_US and pass that new locale to RingtoneManager.getRingtone() (code based from Set Locale programmatically ):
ContextWrapper cw = new ContextWrapper(context);
Context cc = setContextLocale(cw, "en_US"); // This method was copied from the StackOverflow question above ^^
Ringtone defaultRingtone = RingtoneManager.getRingtone(cc, Settings.System.DEFAULT_RINGTONE_URI);
String sss = defaultRingtone.getTitle(cc);
So, yes, this does not work as expected. Any ideas?
Hope you find what you are looking for with following code snippet:
data class Ringtone(
val _id: String,
val title: String
)
val cursor = RingtoneManager(this).cursor
cursor.moveToFirst()
val list = ArrayList<Ringtone>()
do {
list.add(Ringtone(
cursor.getString(0),
cursor.getString(1)
))
} while (cursor.moveToNext())
Log.i(TAG, "ringtones list=$list")
Related
For now, I'm doing this with content resolver. Code:
private fun loadMedia(): List<LocalMusic> {
val list = mutableListOf<LocalMusic>()
val contentResolver = context.contentResolver
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val selection = "${MediaStore.Audio.Media.IS_MUSIC}!= 0"
val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"
var i = 0
contentResolver.query(uri, null, selection, null, sortOrder).use {
if (it != null && it.count > 0) {
while (it.moveToNext()) {
val title = it.getString(it.getColumnIndex(MediaStore.MediaColumns.TITLE))
// Android Studio shows a lint, which tells that MediaStore.MediaColumns.ARTIST
//requires API 30. But in Android Pie it works.
val artist = it.getString(it.getColumnIndex(MediaStore.MediaColumns.ARTIST))
//the same lint as described above(required api 29)
val album = it.getString(it.getColumnIndex(MediaStore.MediaColumns.ALBUM))
// This one is deprecated(fantastic, what to do?).
val source = it.getString(it.getColumnIndex(MediaStore.MediaColumns.DATA))
val duration = it.getLong(it.getColumnIndex(MediaStore.MediaColumns.DURATION))
list.add(LocalMusic(id = "$i", artist = artist, title = title, source = source,
album = album, duration = duration, image = "some uri"))
i++
}
}
}
return list.toList()
}
So, there are some questions:
The main one. Should I use MediaStore.MediaColumns.DATA to get URI anyway? I know that Google tells "sometimes you don't have permissions to work with storage, blah-blah, that's why it's deprecated", but is there a better way(I always have needed permissions)?
About "MediaStore.MediaColumns.ARTIST" and "MediaStore.MediaColumns.ALBUM". Why it requires API 30, if I found its usages in tutorials 5-6 years ago, though there was no API 30 at all? Because of support libraries, migrations, etc? Should I keep using it?
I want to fetch all the images in android device. Therefore I used this code to fetch images-
private fun getAllShownImagesPath(activity: Activity): ArrayList<String>
{
val uri: Uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val listOfAllImages = ArrayList<String>()
var absolutePathOfImage : String
val projection = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
val orderBy = MediaStore.Images.Media.DATE_ADDED
val cursor = activity.contentResolver.query(uri, projection, null, null, orderBy)
val indexData = cursor!!.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
cursor.moveToLast()
while (cursor.moveToPrevious())
{
absolutePathOfImage = cursor.getString(indexData)
listOfAllImages.add(absolutePathOfImage)
}
cursor.close()
return listOfAllImages
}
But this does not returns recently added images like if you downloaded it few hours ago.
This is somehow fixed when you reboot the device or the image you want to access is like one day old.
Note- you can also provide solution in java.
Thanks
Forcefully send broadcast then media cursor will be updated. call this before access media cursor
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Example: I want to get to get string "about_message" from "en"'s locale file (strings.xml) but currently using locale "de" inside the app and when referencing (R.string.about_message) returns the de string value obviously.
Is there a method to do this?
Let's assume you have a <string name="hello">Hello</string> inside your values/strings.xml, which also has a translation (say French) inside values-fr/strings.xml <string name="hello">Bonjour</string>. Normally you'd do the following:
String s = getResources.getString(R.string.hello); // s: "Hello"
To get the "Bonjor" string you'd have to create an alternative resources instance and use it to access the French string by changing to appropirate Locale:
Resources normalResources = getResources();
AssetManager assets = normalResources.getAssets();
DisplayMetrics metrics = normalResources.getDisplayMetrics();
Configuration config = new Configuration(standardResources.getConfiguration());
config.locale = Locale.FRENCH;
Resources frenchResources = new Resources(assets, metrics, config);
String s = defaultResources.getString(R.string.hello); // s: "Bonjour"
Hope this helps.
I am using the Java EWS library to search for appointments in a user's calendar by iCalUid (example iCalUid I have: 040000008200E00074C5B7101A82E00800000000F66E2C0D59A9D001000000000000000010000000F7A6AACB779B00429164F39AE6DD6BB9). Here is my Scala code:
import microsoft.exchange.webservices.data._
import java.net.URI
import java.util.Date
val exchange = {
val service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
service.setUrl(new URI(host))
service.setCredentials(new WebCredentials(user, pass))
service
}
def calendarFolderFor(email: String) = new FolderId(WellKnownFolderName.Calendar, Mailbox.getMailboxFromString(email))
def findMatchingAppointments(iCalUid: String, email: String) = {
val searchFilter = new SearchFilter.IsEqualTo(AppointmentSchema.ICalUid, iCalUid)
exchange.findItems(calendarFolderFor(email), searchFilter, new ItemView(1)).getItems
}
The above code throws this error:
microsoft.exchange.webservices.data.ServiceResponseException: The property can not be used with this type of restriction.
at microsoft.exchange.webservices.data.ServiceResponse.internalThrowIfNecessary(ServiceResponse.java:262) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
at microsoft.exchange.webservices.data.ServiceResponse.throwIfNecessary(ServiceResponse.java:251) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:146) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:807) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:851) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
I then tried adapting the code from this thread:
def findMatchingAppointments(iCalUid: String, email: String) = {
import org.apache.commons.codec.binary.{Hex, Base64}
val searchFilter = new SearchFilter.IsEqualTo(
new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x03, MapiPropertyType.Binary),
Base64.encodeBase64String(Hex.decodeHex(iCalUid.toCharArray))
)
val view = new ItemView(1)
view.setPropertySet(new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.ICalUid))
exchange.findItems(calendarFolderFor(email), searchFilter, view).getItems
}
But, now it does not throw an exception but it does not find the appointment either. I know the appointment exists because I brute-forced and found the appointment using this snippet of code:
def bruteForceFind(start: Date, end: Date, iCalUid: String, email: String) = {
val view = new CalendarView(start, end, 100)
exchange.findAppointments(calendarFolderFor(email), view).getItems
val allAppointments = exchange.findAppointments(calendarFolderFor(email), view).getItems
allAppointments.filter(_.getICalUid == iCalUid)
}
What am I doing wrong? How can I search for appointments for a user given the iCalUid? Working Java/Scala code is okay too.
Apparently there is a difference between a meeting and an appointment. Replacing this line:
new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x03, MapiPropertyType.Binary),
with
new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, 0x03, MapiPropertyType.Binary)
works.
I'm allowing my user to pick a ringtone for notifications in my app. I want to store the URI of the sound along with the human readable title of the sound.
So far the URI code works great:
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
But when I try to get the title, and set it as a button text, I don't get anything. Seems to have no title?
String title = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
button.setText(title);
But my button text is empty. If I do:
button.setText(uri.toString());
then I see the uri perfectly. Should I just try to get the title from the URI? Thanks
This should get it:
Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
String title = ringtone.getTitle(this);
Refer to http://developer.android.com/reference/android/media/Ringtone.html for the documentation, but the short story: Ringtone.getTitle(Context ctx);
I personally had a serious performance problem when I tried the accepted answer, it took about 2 seconds to just load a list of 30 ringtones. I changed it a bit and it works about 10x faster:
uri = ringtoneMgr.getRingtoneUri(cursor.getPosition());
ContentResolver cr = getContext().getContentResolver();
String[] projection = {MediaStore.MediaColumns.TITLE};
String title;
Cursor cur = cr.query(uri, projection, null, null, null);
if (cur != null) {
if (cur.moveToFirst()) {
title = cur.getString(0);
cur.close();
I had problems with 'MediaPlayer finalized without being released'. I use this:
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
returnCursor.moveToFirst();
String title = returnCursor.getString(returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
returnCursor.close();
Refer to https://developer.android.com/training/secure-file-sharing/retrieve-info.html for the documentation.