From 6e595ddebaafbdd2390fde3e80cebeb259db6c07 Mon Sep 17 00:00:00 2001 From: Leonidius20 <25536411+Leonidius20@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:12:38 +0300 Subject: [PATCH] fixed crashing when trying to record on android 5, fixing MediaStore usage --- .../recorder/data/recorder/RecorderService.kt | 18 ++++++++++++++++-- .../RecordingsListRepository.kt | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/github/leonidius20/recorder/data/recorder/RecorderService.kt b/app/src/main/java/io/github/leonidius20/recorder/data/recorder/RecorderService.kt index 85050ae..c737c42 100644 --- a/app/src/main/java/io/github/leonidius20/recorder/data/recorder/RecorderService.kt +++ b/app/src/main/java/io/github/leonidius20/recorder/data/recorder/RecorderService.kt @@ -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 @@ -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 @@ -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")!! @@ -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!! } diff --git a/app/src/main/java/io/github/leonidius20/recorder/data/recordings_list/RecordingsListRepository.kt b/app/src/main/java/io/github/leonidius20/recorder/data/recordings_list/RecordingsListRepository.kt index e67476e..b4766dc 100644 --- a/app/src/main/java/io/github/leonidius20/recorder/data/recordings_list/RecordingsListRepository.kt +++ b/app/src/main/java/io/github/leonidius20/recorder/data/recordings_list/RecordingsListRepository.kt @@ -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 }