大约有 800 项符合查询结果(耗时:0.0332秒) [XML]
How to determine MIME type of file in android?
...lowercase!
Update (19.03.2018)
Bonus: Above methods as a less verbose Kotlin extension function:
fun File.getMimeType(fallback: String = "image/*"): String {
return MimeTypeMap.getFileExtensionFromUrl(toString())
?.run { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLow...
How to get hosting Activity from a view?
...
I like this solution written in Kotlin
tailrec fun Context?.getActivity(): Activity? = when (this) {
is Activity -> this
else -> (this as? ContextWrapper)?.baseContext?.getActivity()
}
Usage in View class
context.getActivity()
Decompiled cod...
Pick a random value from an enum?
...
Simple Kotlin Solution
MyEnum.values().random()
random() is a default extension function included in base Kotlin on the Collection object. Kotlin Documentation Link
If you'd like to simplify it with an extension function, try th...
Gson - convert from Json to a typed ArrayList
...arameters, such as Class<?> or List<? extends CharSequence>.
Kotlin:
If you need to do it in Kotlin you can do it like this:
val myType = object : TypeToken<List<JsonLong>>() {}.type
val logs = gson.fromJson<List<JsonLong>>(br, myType)
Or you can see this a...
Running code in main thread from another thread
...
Kotlin versions
When you are on an activity, then use
runOnUiThread {
//code that runs in main
}
When you have activity context, mContext then use
mContext.runOnUiThread {
//code that runs in main
}
When you are in ...
How to make a copy of a file in android?
...
Kotlin extension for it
fun File.copyTo(file: File) {
inputStream().use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
}
...
Android Bitmap to Base64 String
...hat file to a Base64 string.
Here is how to do that in easy copy-paste (in Kotlin). Note you must close the base64FilterStream to truly flush its internal buffer.
fun convertImageFileToBase64(imageFile: File): String {
return FileInputStream(imageFile).use { inputStream ->
ByteArray...
Computed read-only property vs function in Swift
...
Well, you can apply Kotlin 's advices https://kotlinlang.org/docs/reference/coding-conventions.html#functions-vs-properties.
In some cases functions with no arguments might be interchangeable
with read-only properties. Although the semant...
How do I create ColorStateList programmatically?
...
Here's an example of how to create a ColorList programmatically in Kotlin:
val colorList = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_enabled), // Disabled
intArrayOf(android.R.attr.state_enabled) // Enabled
),
intAr...
Is there a method that works like start fragment for result?
...nceState: Bundle?) {
super.onCreate(savedInstanceState)
// Use the Kotlin extension in the fragment-ktx artifact
setResultListener("requestKey") { key, bundle ->
// We use a String here, but any type that can be put in a Bundle is supported
val result = bundle.getStrin...