Saving and Reading Bitmaps/Images from Internal memory in Android

前端 未结 7 1369
傲寒
傲寒 2020-11-22 02:59

What I want to do, is to save an image to the internal memory of the phone (Not The SD Card).

How can I do it?

I have got the image directly

7条回答
  •  攒了一身酷
    2020-11-22 03:25

    For Kotlin users, I created a ImageStorageManager class which will handle save, get and delete actions for images easily:

    class ImageStorageManager {
        companion object {
            fun saveToInternalStorage(context: Context, bitmapImage: Bitmap, imageFileName: String): String {
                context.openFileOutput(imageFileName, Context.MODE_PRIVATE).use { fos ->
                    bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, fos)
                }
                return context.filesDir.absolutePath
            }
    
            fun getImageFromInternalStorage(context: Context, imageFileName: String): Bitmap? {
                val directory = context.filesDir
                val file = File(directory, imageFileName)
                return BitmapFactory.decodeStream(FileInputStream(file))
            }
    
            fun deleteImageFromInternalStorage(context: Context, imageFileName: String): Boolean {
                val dir = context.filesDir
                val file = File(dir, imageFileName)
                return file.delete()
            }
        }
    }
    

    Read more here

提交回复
热议问题