Repository: skyline-emu/skyline
File: app/src/main/java/emu/skyline/preference/ProfilePicturePreference.kt (Kotlin)

Code before:
                    File("$profilePictureDir/$profilePictureName").delete()
                }
                PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, "No picture selected").apply()
            }
            notifyChanged()
        } catch (e : Exception) {
            e.printStackTrace()
        }
    }

    init {
        summaryProvider = SummaryProvider<ProfilePicturePreference> { preference ->
            Uri.decode(preference.getPersistedString("No picture selected"))
        }
    }

    override fun onClick() = pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))

    /**
     * Given a bitmap, saves it in the specified location
     */
    private fun StoreBitmap(bitmap : Bitmap, filePath : String) {
        try {
            // Create the file where the bitmap will be stored
            val file = File(filePath)
            file.createNewFile()
            // Store bitmap as JPEG
            val outputFile = FileOutputStream(file)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputFile)
            outputFile.flush()
            outputFile.close()
        } catch (e : Exception) {
            e.printStackTrace()
        }
    }
}


Review comment (suggestion):
I find the choice of ordering here quite funny, but we generally would do it like `preview != null`.

Code after:
                if (File(profilePicture).exists()) {
                    File(profilePicture).delete()
                }
                PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, "No picture selected").apply()
            }
            updatePreview()
            notifyChanged()
        } catch (e : Exception) {
            e.printStackTrace()
        }
    }

    init {
        summaryProvider = SummaryProvider<ProfilePicturePreference> { preference ->
            Uri.decode(preference.getPersistedString("No picture selected"))
        }
        updatePreview()
    }

    override fun onClick() = pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))

    private fun updatePreview() {
        var drawable: BitmapDrawable? = null
        if (File(profilePicture).exists()) {
            val preview = BitmapFactory.decodeFile(profilePicture)
            if (preview != null) {
                drawable = BitmapDrawable(context.resources, preview)
            }
        }
        icon = drawable
    }

    /**
     * Given a bitmap, saves it in the specified location
     */
    private fun storeBitmap(bitmap : Bitmap, filePath : String) {
        try {
            // Create the file where the bitmap will be stored
            val file = File(filePath)
            file.createNewFile()
            // Store bitmap as JPEG
            val outputFile = FileOutputStream(file)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputFile)
            outputFile.flush()
            outputFile.close()
        } catch (e : Exception) {
            e.printStackTrace()
        } finally {
            bitmap.recycle()
        }
    }