Search code examples
androidkotlincameragallery

How to add a photo to android built-in Gallery in kotlin?


In my application I take a photo (in a Fragment) and then I am trying to save it into the Gallery. When I go to the Gallery it is not there. It only stays in the app folder. I was following the android docs. Everything else worked fine.

I have tried this code from the docs but ACTION_MEDIA_SCANNER_SCAN_FILE is deprecated. And I was unable to find any alternatives which were not deprecated or below API 25.

   private fun galleryAddPic() {
        val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
        val f: File = File(myPath)
        val contentUri = Uri.fromFile(f)
        mediaScanIntent.data = contentUri
        requireContext().sendBroadcast(mediaScanIntent)
        Toast.makeText(requireContext(), "Picture Added to Gallery", Toast.LENGTH_SHORT).show()
    }

I even tried this but insertImage is also deprecated.

MediaStore.Images.Media.insertImage

Are there any other alternatives?


Solution

  • The perfect way of inserting an image in gallery is as follow :

        override suspend fun saveCameraImage(bitmap: Bitmap)
        {
                try {
                    val collection = sdk29AndUp {
                        MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
                    } ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                    val dirDest = File(
                        Environment.DIRECTORY_PICTURES,
                        context.getString(R.string.app_name) 
                    )
                    val date = System.currentTimeMillis()
                    val fileName = "$date.${IMAGE_EXTENSIONS}"
    
    
                    val contentValues = ContentValues().apply {
                        put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
                        put(MediaStore.MediaColumns.MIME_TYPE, "image/$IMAGE_EXTENSIONS")
                        put(MediaStore.MediaColumns.DATE_ADDED, date)
                        put(MediaStore.MediaColumns.DATE_MODIFIED, date)
                        put(MediaStore.MediaColumns.SIZE, bitmap.byteCount)
                        put(MediaStore.MediaColumns.WIDTH, bitmap.width)
                        put(MediaStore.MediaColumns.HEIGHT, bitmap.height)
                       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        
                        put(MediaStore.MediaColumns.RELATIVE_PATH, "$dirDest${File.separator}")
                        put(MediaStore.Images.Media.IS_PENDING, 1)
                    }
    }
    
                    val imageUri = context.contentResolver.insert(collection, contentValues)
    
    
                    withContext(Dispatchers.IO) {
    
                        imageUri?.let { uri ->
                            context.contentResolver.openOutputStream(uri, "w").use { out ->
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
                            }
    
                            contentValues.clear()
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        contentValues.put(MediaStore.Images.Media.IS_PENDING, 0)}
                            context.contentResolver.update(uri, contentValues, null, null)
                        }
                    }
    
    
    
                } catch (e: FileNotFoundException) {
                    
                }
            
        }