Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.areeb.whatsappstatussaver.ui.DetailScreen.fragments
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.areeb.whatsappstatussaver.databinding.FragmentDetailBinding
Expand All @@ -15,7 +17,14 @@ import com.areeb.whatsappstatussaver.utils.constants.Constants.TARGET_DIRECTORY.
import com.areeb.whatsappstatussaver.utils.constants.Constants.TARGET_DIRECTORY.SHARING.Companion.SCREEN
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream

@AndroidEntryPoint
class DetailFragment : BaseFragments(), View.OnClickListener {
Expand Down Expand Up @@ -80,14 +89,18 @@ class DetailFragment : BaseFragments(), View.OnClickListener {
if (arguments?.getInt(SCREEN) == 1) {
downloadImage()
} else {
showToast("coming soon")
downloadVideo(
Uri.parse(
arguments?.getString(FRAGMENT_IMAGE_URI).toString(),
),
)
}
}
}
}
}

private suspend fun downloadImage() {
private fun downloadImage() {
val drawable = binding.detailImage.drawable as? BitmapDrawable
val bitmap = drawable?.bitmap

Expand All @@ -96,6 +109,50 @@ class DetailFragment : BaseFragments(), View.OnClickListener {
}
}

private fun downloadVideo(videoUri: Uri) {
lifecycleScope.launch(Dispatchers.IO) {
val inputStream: InputStream? =
requireActivity().contentResolver.openInputStream(videoUri)
val filename = "my_video.mp4" // Specify the desired filename with the correct extension
val outputFile =
File(requireActivity().getExternalFilesDir(Environment.DIRECTORY_MOVIES), filename)

try {
val outputStream: OutputStream = FileOutputStream(outputFile)

inputStream?.let {
val buffer = ByteArray(4096)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
}
outputStream.flush()

// Download successful, show a Toast message
withContext(Dispatchers.Main) {
Toast.makeText(
requireContext(),
"Video downloaded successfully!",
Toast.LENGTH_SHORT,
).show()
}
}
} catch (e: IOException) {
e.printStackTrace()
// Show a Toast message for any download errors
withContext(Dispatchers.Main) {
Toast.makeText(
requireContext(),
"Failed to download video.",
Toast.LENGTH_SHORT,
).show()
}
} finally {
inputStream?.close()
}
}
}

override fun onDestroy() {
super.onDestroy()
binding.videoView.setOnPreparedListener {
Expand Down