messenger-android

Android graphical user interfaces for GNUnet Messenger
Log | Files | Refs | README | LICENSE

AvatarStorageUtil.kt (2667B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2021--2025 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author t3sserakt
     22  * @file GNUnetMessenger/app/src/main/java/org/gnunet/gnunetmessenger/ui/util/AvatarStorageUtil.kt
     23  */
     24 
     25 package org.gnunet.gnunetmessenger.util
     26 
     27 import android.content.Context
     28 import android.graphics.Bitmap
     29 import android.graphics.BitmapFactory
     30 import android.net.Uri
     31 import java.io.File
     32 import java.io.FileOutputStream
     33 
     34 object AvatarStorageUtil {
     35 
     36     fun saveAvatarFromUri(context: Context, uri: Uri, accountKey: String): Boolean {
     37         val filename = getAvatarFilename(accountKey)
     38         val file = File(context.filesDir, filename)
     39 
     40         return try {
     41             val inputStream = context.contentResolver.openInputStream(uri)
     42             val outputStream = FileOutputStream(file)
     43 
     44             inputStream?.use { input ->
     45                 outputStream.use { output ->
     46                     input.copyTo(output)
     47                 }
     48             }
     49             true
     50         } catch (e: Exception) {
     51             e.printStackTrace()
     52             false
     53         }
     54     }
     55 
     56     fun saveAvatarBitmap(context: Context, bitmap: Bitmap, accountKey: String): Boolean {
     57         val filename = getAvatarFilename(accountKey)
     58         val file = File(context.filesDir, filename)
     59 
     60         return try {
     61             FileOutputStream(file).use { output ->
     62                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)
     63             }
     64             true
     65         } catch (e: Exception) {
     66             e.printStackTrace()
     67             false
     68         }
     69     }
     70 
     71     fun loadAvatar(context: Context, accountKey: String): Bitmap? {
     72         val filename = getAvatarFilename(accountKey)
     73         val file = File(context.filesDir, filename)
     74 
     75         return if (file.exists()) {
     76             BitmapFactory.decodeFile(file.absolutePath)
     77         } else {
     78             null
     79         }
     80     }
     81 
     82     private fun getAvatarFilename(accountKey: String): String {
     83         return "avatar_$accountKey.png"
     84     }
     85 }