Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
1
Indexable
Never
holder.itemView.setOnLongClickListener {
            val inputStream: InputStream
            val fileOutputStream: FileOutputStream
            try {
                inputStream =
                    holder.itemView.context.resources.openRawResource(replikList.get(position).replikYolu)
                fileOutputStream = FileOutputStream(
                    File(
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                        "${replikList.get(position).replikIsmi}.mp3"
                    )
                )
                val buffer = ByteArray(1024)
                var length: Int
                while (inputStream.read(buffer).also { length = it } > 0) {
                    fileOutputStream.write(buffer, 0, length)
                }
                inputStream.close()
                fileOutputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

            val intent = Intent(Intent.ACTION_SEND)
            val file = File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "${replikList.get(position).replikIsmi}.mp3"
            )
            val uri = FileProvider.getUriForFile(
                holder.itemView.context,
                holder.itemView.context.applicationContext.packageName + ".provider",
                file
            )
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.type = "audio/*"
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            holder.itemView.context.startActivity(Intent.createChooser(intent, "Share sound"))
            true
        }