How to resolve weird and incomplete Uri & Path received while directly sharing image from android gallery to my flutter app
Asked Answered
G

1

15

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?

Gymnosophist answered 3/2, 2022 at 12:35 Comment(4)
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 #13209994 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 ?Come
use Uri.parse('url')Forbade
Are you use Image asset or Image file?Forbade
could you possibly provide the specific file path for the content:// URI?Rubie
M
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);
}
Maneuver answered 12/2, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.