Improved image saving

This commit is contained in:
Hemanth S 2020-05-21 04:53:40 +05:30
parent e015741470
commit 7db515bb16
3 changed files with 13 additions and 28 deletions

View file

@ -153,29 +153,29 @@ class UserInfoActivity : AbsBaseActivity() {
isFromMemoryCache: Boolean,
isFirstResource: Boolean
): Boolean {
resource?.let { saveBannerImage(it) }
resource?.let { saveImage(it, USER_BANNER) }
return false
}
})
.into(bannerImage)
}
private fun saveBannerImage(bitmap: Bitmap) {
private fun saveImage(bitmap: Bitmap, fileName: String) {
CoroutineScope(Dispatchers.IO).launch() {
val appDir = applicationContext.filesDir
val file = File(appDir, USER_BANNER)
val file = File(appDir, fileName)
var successful = false
try {
val os = BufferedOutputStream(FileOutputStream(file))
successful = ImageUtil.resizeBitmap(bitmap, 2048)
.compress(Bitmap.CompressFormat.WEBP, 100, os)
os.close()
withContext(Dispatchers.IO) { os.close() }
} catch (e: IOException) {
e.printStackTrace()
}
if (successful) {
withContext(Dispatchers.Main) {
Toast.makeText(this@UserInfoActivity, "Done", Toast.LENGTH_SHORT).show()
Toast.makeText(this@UserInfoActivity, "Updated", Toast.LENGTH_SHORT).show()
}
}
}
@ -203,33 +203,13 @@ class UserInfoActivity : AbsBaseActivity() {
isFromMemoryCache: Boolean,
isFirstResource: Boolean
): Boolean {
resource?.let { saveImage(it) }
resource?.let { saveImage(it, USER_PROFILE) }
return false
}
})
.into(userImage)
}
private fun saveImage(bitmap: Bitmap) {
CoroutineScope(Dispatchers.IO).launch() {
val appDir = applicationContext.filesDir
val file = File(appDir, USER_PROFILE)
var successful = false
try {
val os = BufferedOutputStream(FileOutputStream(file))
successful = ImageUtil.resizeBitmap(bitmap, 2048)
.compress(Bitmap.CompressFormat.WEBP, 100, os)
os.close()
} catch (e: IOException) {
e.printStackTrace()
}
if (successful) {
withContext(Dispatchers.Main) {
Toast.makeText(this@UserInfoActivity, "Done", Toast.LENGTH_SHORT).show()
}
}
}
}
companion object {
private const val PICK_IMAGE_REQUEST = 9002

View file

@ -45,11 +45,13 @@
<item>@string/dark_theme_name</item>
<item>@string/follow_system</item>
</string-array>
<string-array name="pref_general_theme_list_values">
<item>light</item>
<item>dark</item>
<item>auto</item>
</string-array>
<string-array name="pref_grid_style_list_titles">
<item>@string/normal_style</item>
<item>@string/card_style</item>
@ -82,6 +84,7 @@
<item>@string/card_color_style</item>
<item>@string/card_style</item>
<item>@string/tiny_card_style</item>
<item>@string/card_square</item>
</string-array>
<string-array name="pref_home_grid_style_list_values">
@ -89,6 +92,7 @@
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<array name="pref_home_grid_style_layout">
@ -96,16 +100,15 @@
<item>@layout/item_artist_square</item>
<item>@layout/item_album_card</item>
<item>@layout/item_artist_card</item>
<item>@layout/item_image</item>
</array>
<array name="pref_auto_download_images_values">
<item>always</item>
<item>only_wifi</item>
<item>never</item>
</array>
<string-array name="pref_playlists_last_added_interval_titles">
<item>@string/today</item>
<item>@string/this_week</item>

View file

@ -164,6 +164,8 @@
<string name="card_style">Card</string>
<string name="card_square">Square Card</string>
<string name="carousal">Carousel</string>
<string name="carousal_effect_on_now_playing_screen">Carousel effect on the now playing screen</string>