-
-
Notifications
You must be signed in to change notification settings - Fork 20
Preserve original file name on API 33+ by switching to SAF picker #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -416,7 +416,43 @@ class CompressorViewModel(application: Application) : AndroidViewModel(applicati | |
| private var compressionJob: Job? = null | ||
| private var activeTransformer: Transformer? = null | ||
|
|
||
| private fun queryDisplayName(context: Context, uri: Uri): String? { | ||
| val raw = try { | ||
| context.contentResolver.query( | ||
| uri, | ||
| arrayOf(android.provider.OpenableColumns.DISPLAY_NAME), | ||
| null, null, null | ||
| )?.use { cursor -> | ||
| if (cursor.moveToFirst()) { | ||
| val idx = cursor.getColumnIndex(android.provider.OpenableColumns.DISPLAY_NAME) | ||
| if (idx >= 0) cursor.getString(idx) else null | ||
| } else null | ||
| } | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| null | ||
| } | ||
| // Reject Photo Picker synthetic IDs (e.g. "1000040501") — these are MediaStore IDs, | ||
| // not real filenames. The picker hides the actual name for privacy on Android 13+. | ||
| if (raw.isNullOrBlank() || raw.matches(Regex("""^\d{6,}$"""))) return null | ||
| return raw | ||
| } | ||
|
|
||
| private fun sanitizeFilename(name: String): String = | ||
| name.replace(Regex("""[/\\:*?"<>|\x00-\x1F]"""), "_").trim().ifBlank { "Video" } | ||
|
|
||
| private fun makeOutputName(originalName: String?): String { | ||
| val base = originalName?.substringBeforeLast(".")?.takeIf { it.isNotBlank() } | ||
| ?.let { sanitizeFilename(it) } | ||
| ?: "Video_${System.currentTimeMillis()}" | ||
| return "${base}_Compressed.mp4" | ||
| } | ||
|
|
||
| fun updateSelectedUri(context: Context, uri: Uri) { | ||
| // Resolve the display name independently — if metadata extraction throws below | ||
| // (HDR videos, unsupported codecs, slow URIs), we still keep the original name. | ||
| val originalName = queryDisplayName(context, uri) | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unneeded linebreak |
||
| var size = 0L | ||
| var width = 0 | ||
| var height = 0 | ||
|
|
@@ -425,8 +461,7 @@ class CompressorViewModel(application: Application) : AndroidViewModel(applicati | |
| var fps = 30f | ||
| var videoMime: String? = null | ||
| var duration = 0L | ||
| var originalName: String? = null | ||
|
|
||
|
|
||
| try { | ||
| audioBitrate = getAudioBitrate(context, uri) | ||
| val videoInfo = getVideoTrackInfo(context, uri) | ||
|
|
@@ -460,15 +495,6 @@ class CompressorViewModel(application: Application) : AndroidViewModel(applicati | |
| fps = 30f | ||
| } | ||
|
|
||
| val cursor = context.contentResolver.query(uri, null, null, null, null) | ||
| if (cursor != null && cursor.moveToFirst()) { | ||
| val nameIndex = cursor.getColumnIndex(android.provider.OpenableColumns.DISPLAY_NAME) | ||
| if (nameIndex != -1) { | ||
| originalName = cursor.getString(nameIndex) | ||
| } | ||
| cursor.close() | ||
| } | ||
|
|
||
| retriever.release() | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
|
|
@@ -713,8 +739,7 @@ class CompressorViewModel(application: Application) : AndroidViewModel(applicati | |
|
|
||
| val outputDir = File(context.cacheDir, "compressed_videos") | ||
| outputDir.mkdirs() | ||
| val baseName = currentState.originalName?.substringBeforeLast(".") ?: "Compressed_${System.currentTimeMillis()}" | ||
| val outputFile = File(outputDir, "${baseName}_Compressed.mp4") | ||
| val outputFile = File(outputDir, makeOutputName(currentState.originalName)) | ||
| if (outputFile.exists()) { | ||
| outputFile.delete() | ||
| } | ||
|
|
@@ -1117,12 +1142,7 @@ class CompressorViewModel(application: Application) : AndroidViewModel(applicati | |
| return@launch | ||
| } | ||
|
|
||
| val targetName = if (currentState.originalName != null) { | ||
| val nameWithoutExt = currentState.originalName.substringBeforeLast(".") | ||
| "${nameWithoutExt}_Compressed.mp4" | ||
| } else { | ||
| "Compressed_${System.currentTimeMillis()}.mp4" | ||
| } | ||
| val targetName = makeOutputName(currentState.originalName) | ||
|
|
||
| val values = ContentValues().apply { | ||
| put(MediaStore.Video.Media.DISPLAY_NAME, targetName) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.rememberLauncherForActivityResult | ||
| import androidx.activity.compose.setContent | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.activity.result.PickVisualMediaRequest | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.activity.viewModels | ||
| import androidx.compose.animation.AnimatedContent | ||
|
|
@@ -160,8 +159,14 @@ fun CompressorApp(viewModel: CompressorViewModel) { | |
| } | ||
| } | ||
|
|
||
| val pickMedia = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri -> | ||
| // SAF-based picker — preserves the real file name (Photo Picker hides it for privacy on API 33+). | ||
| val pickMedia = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> | ||
| if (uri != null) { | ||
| try { | ||
| context.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
| } catch (_: SecurityException) { | ||
| // Some providers don't grant persistable URIs; the URI is still readable for this session. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? Is this tested? There shouldn't be any SecurityExceptions |
||
| } | ||
| viewModel.updateSelectedUri(context, uri) | ||
| } | ||
| } | ||
|
|
@@ -241,7 +246,7 @@ fun CompressorApp(viewModel: CompressorViewModel) { | |
| when(index) { | ||
| 0 -> EmptyScreen( | ||
| totalSaved = state.formattedTotalSaved, | ||
| onPick = { pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly)) } | ||
| onPick = { pickMedia.launch(arrayOf("video/*")) } | ||
| ) | ||
| 2 -> { | ||
| if (state.error != null) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this, it's not needed