10

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?

  • Could you share the code of `FileDirectory.getPathFromUri()`? To be honest `content://` 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 https://stackoverflow.com/questions/13209494/how-to-get-the-full-file-path-from-uri for hacks to retrieve file URL from `content://` 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 use `Uri` instead of `File` ? – tonymanou Feb 08 '22 at 13:32
  • use Uri.parse('url') – Mouaz M Shahmeh Mar 14 '22 at 20:14
  • Are you use Image asset or Image file? – Mouaz M Shahmeh Mar 14 '22 at 20:24

1 Answers1

0

You can use uri_to_file plugin to fetch the path from content://uri. To use, simply parse the uri and use toFile(uri)

import 'package:uri_to_file/uri_to_file.dart';

try {
  String uriPath = 'content://com.android.providers.downloads.documents/document/624';
  Uri uri = Uri.parse(uriPath);
  File file = await toFile(uri);
} catch (e) {
  debugPrint(e);
}
Omatt
  • 8,564
  • 2
  • 42
  • 144