fun URL.toBitmap(): Bitmap? {
return try {
BitmapFactory.decodeStream(openStream())
} catch (e: IOException) {
null
}
}
fun Bitmap.saveToInternalStorage(context: Context): Uri? {
val wrapper = ContextWrapper(context)
var file = wrapper.getDir("images", Context.MODE_PRIVATE)
file = File(file, "${UUID.randomUUID()}.jpg")
return try {
FileOutputStream(file).use { stream ->
compress(Bitmap.CompressFormat.JPEG, 100, stream)
stream.flush()
}
Uri.parse(file.absolutePath)
} catch (e: IOException) {
e.printStackTrace()
null
}
}
Last active
December 29, 2021 19:02
-
-
Save Jswizzy/aedf54a42fcaaf2170870263da359ada to your computer and use it in GitHub Desktop.
android utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks the Internet connection and performs an action if it's active. | |
*/ | |
class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) { | |
inline fun performIfConnectedToInternet(action: () -> Unit) { | |
if (hasInternetConnection()) { | |
action() | |
} | |
} | |
fun hasInternetConnection(): Boolean { | |
val network = connectivityManager?.activeNetwork ?: return false | |
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false | |
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || | |
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || | |
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) | |
} | |
} | |
// check this out instead https://stackoverflow.com/questions/53532406/activenetworkinfo-type-is-deprecated-in-api-level-28 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import okio.ByteString.Companion.decodeBase64 | |
import org.spongycastle.util.encoders.Base64 | |
import java.security.KeyFactory | |
import java.security.PrivateKey | |
import java.security.spec.PKCS8EncodedKeySpec | |
private const val prefix = "-----BEGIN PRIVATE KEY-----\n" | |
private const val postfix = "\n-----END PRIVATE KEY-----" | |
fun PrivateKey.toPkcs8Pem(): String { | |
val privateKeyBase64 = String(Base64.encode(this.encoded)) | |
return privateKeyBase64.chunked(64).joinToString( | |
separator = "\n", | |
prefix = prefix, | |
postfix = postfix | |
) | |
} | |
fun String.toPrivateKey(): PrivateKey { | |
return this | |
.replace(prefix, "") | |
.replace(postfix, "") | |
.decodeBase64()?.let { byteString -> | |
KeyFactory.getInstance("RSA").generatePrivate( | |
PKCS8EncodedKeySpec(byteString.toByteArray()) | |
) | |
} ?: throw IllegalStateException("Failed to parse PrivateKey") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{SomeClass}Ext.kt
remember{Some}State()