I have written a functionality in Kotlin for the flutter app to share media files directly from the android phone gallery to the app.
Working fine for many devices I have tested like Realme 3 Pro, Oneplus 6t, Samsung m40.
But, for Asus Zenfone Max Pro M2
on sharing any media file received weird and incomplete URI
& path
Logs from Kotlin Code
D/URI =====(26032) : content://com.android.providers.downloads.documents/document/624
D/Path =====(26032): /document/624
Kotlin Code
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.binding = binding
binding.addOnNewIntentListener(this)
handleIntent(binding.activity.intent, true)
}
----------------------------------------------------------------------
private fun handleIntent(intent: Intent, initial: Boolean) {
when {
(intent.type?.startsWith("text") != true)
&& (intent.action == Intent.ACTION_SEND
|| intent.action == Intent.ACTION_SEND_MULTIPLE) -> { // Sharing images or videos
val value = getMediaUris(intent)
if (initial) initialMedia = value
latestMedia = value
eventSinkMedia?.success(latestMedia?.toString())
}
...
----------------------------------------------------------------------
private fun getMediaUris(intent: Intent?): JSONArray? {
if (intent == null) return null
return when (intent.action) {
Intent.ACTION_SEND -> {
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
Log.d("URI =====",uri)
val path = uri?.let{ FileDirectory.getPathFromUri(applicationContext, it) } ?: uri?.path
Log.d("Path =====",path)
if (path != null) {
...
Device Info
Device Name: Asus Zenfone Max Pro M2
Android Version: 9
External SD card: present
How to resolve this URI and get a proper path with filename and extension?
FileDirectory.getPathFromUri()
? To be honestcontent://
URI does not necessarily resolves to a file on the filesystem, not to mention that you may not have access to this file. You can see #13209994 for hacks to retrieve file URL fromcontent://
URI, but Asus' implementation of the document provider might be different from other brands and could prevent you from converting the URI to a file path... Do you have the possibility to change your app to useUri
instead ofFile
? – Comecontent://
URI? – Rubie