Skip to content

Commit

Permalink
Fix filename when saving a shared file
Browse files Browse the repository at this point in the history
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 FossifyOrg#37.
  • Loading branch information
tom93 committed Apr 20, 2024
1 parent 666bfb6 commit 75482c3
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -35,10 +35,10 @@ class SaveAsActivity : SimpleActivity() {
}
}

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

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

0 comments on commit 75482c3

Please sign in to comment.