0

in my adapter i have add this code

 holder.binding.saveButton.setOnClickListener {
            mediaPlayer.start()
            if (ContextCompat.checkSelfPermission(holder.itemView.context,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(holder.itemView.context as Activity,
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    REQUEST_WRITE_STORAGE)
            } else {
                val parentView = holder.itemView.findViewById<LinearLayout>(R.id.itemViewe)
                parentView.isDrawingCacheEnabled = true
                val bitmap = parentView.getDrawingCache()

                // Create a file to save the image
                val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                val image = File.createTempFile("your_image_name", ".jpg", imagesDir)

                // Compress the bitmap and save it to the file
                val stream = FileOutputStream(image)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
                stream.flush()
                stream.close()

                // Clear the drawing cache
                parentView.destroyDrawingCache()

                // Add the image to the system gallery
                val values = ContentValues()
                values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
                values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                values.put(MediaStore.MediaColumns.DATA, image.absolutePath)
                holder.itemView.context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

                // Show a toast message to indicate that the image has been saved
                Toast.makeText(holder.itemView.context, "Status Saved", Toast.LENGTH_SHORT).show()
            }
        }

when click on save button in recyclerview show permisiion to allow and after show toast status saved but in gallery its not showing ..... in my case my device android version 11 so image show in gallery but in version 12 when save button clicked show toast and show blank in gallery not full image.

it work on some version and not work in other versions but i want to work it for all device how can i do it

0 Answers0