Skip to content

Commit

Permalink
fixed crashing when trying to record on android 5, fixing MediaStore …
Browse files Browse the repository at this point in the history
…usage
  • Loading branch information
Leonidius20 committed Aug 20, 2024
1 parent 6cbb78d commit 6e595dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import android.os.IBinder
import android.os.ParcelFileDescriptor
import android.provider.MediaStore
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.MimeTypes
import com.permissionx.guolindev.PermissionX
import com.yashovardhan99.timeit.Stopwatch
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -37,6 +40,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
Expand Down Expand Up @@ -162,6 +166,8 @@ class RecorderService : LifecycleService() {
val fileName = dateFormat.format(Date(System.currentTimeMillis()))

fileUri = getRecFileUri(fileName, fileFormat.mimeType)
Log.d("RECSERVICE", "file uri $fileUri")
// should be "content" also check EXTERNAL vs INTERNAL storage
descriptor = applicationContext.contentResolver.openFileDescriptor(fileUri!!, "w")!!


Expand Down Expand Up @@ -319,12 +325,20 @@ class RecorderService : LifecycleService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.MediaColumns.RELATIVE_PATH, "Recordings/RecordingStudio")
} else {
put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().absolutePath + "/Recordings/RecordingStudio/" + name)
val folderPath = Environment.getExternalStorageDirectory().absolutePath + "/Music/RecordingStudio/"
val fullFileName = "$name.${MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType)}"
put(
MediaStore.MediaColumns.DATA,
folderPath + fullFileName
)
val folder = File(folderPath)
if (!folder.exists()) folder.mkdirs()
}

}

val uri = resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues)
val uri = resolver.insert(//MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues)

return uri!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RecordingsListRepository @Inject constructor(
MediaStore.Audio.Media.RELATIVE_PATH

val selectionColumnValue = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val path = Environment.getExternalStorageDirectory().absolutePath + "/Recordings/RecordingStudio/" + "%"
val path = Environment.getExternalStorageDirectory().absolutePath + "/Music/RecordingStudio/" + "%"
Log.d("RecListRepo", "Path: $path")
path
}
Expand Down

0 comments on commit 6e595dd

Please sign in to comment.