Skip to content

Commit 75482c3

Browse files
committed
Fix filename when saving a shared file
Previously, the code used the last path component of the content URI, but that isn't necessarily the filename -- it can be a numeric ID. Switch to the helper function getFilenameFromContentUri(), which uses ContentResolver to read the DISPLAY_NAME column. (If that fails, we fall back to the last component of the content URI as before, because we don't have anything better to use.) Also improve the way the MIME type is determined. Previously it was just based on the file extension. Switch to first query the ContentResolver, then try the type of the Intent (which is supposed to be set to the MIME type of the data for ACTION_SEND, but may be set to "*/*" if the type is unknown), and only then fall back to using the file extension. Fixes #37.
1 parent 666bfb6 commit 75482c3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class SaveAsActivity : SimpleActivity() {
3535
}
3636
}
3737

38-
val source = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
39-
val mimeType = source!!.toString().getMimeType()
38+
val source = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)!!
39+
val filename = getFilenameFromContentUri(source) ?: source.toString().getFilenameFromPath()
40+
val mimeType = contentResolver.getType(source) ?: intent.getType()?.takeIf { it != "*/*" } ?: filename.getMimeType()
4041
val inputStream = contentResolver.openInputStream(source)
41-
val filename = source.toString().getFilenameFromPath()
4242

4343
val destinationPath = "$destination/$filename"
4444
val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!!

0 commit comments

Comments
 (0)