question

TomerZ-1923 avatar image
0 Votes"
TomerZ-1923 asked ColeXia-MSFT commented

Unable to get path for file shared from another app

I have a an app with share option that accepts files.
In order to resolve the actual file path (for future FileStream instance) i am using the "famous" answer from SO (https://stackoverflow.com/questions/17546101/get-real-path-for-uri-android).

This solution works fine when sending file from the gallery or "Files" in my Android device.
The URI i receive in the intent is content://0@media/external/images/media/84042


The issue is when i try to share a file from Whatsapp or call recording app, the path i receive is content://com.nll.acr.provider.AttachmentProvider/1d20210503121810pnull.m4a

it fails at:
cursor = context.ContentResolver.Query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.MoveToFirst())
{
int index = cursor.GetColumnIndexOrThrow(column);
return cursor.GetString(index); <------- The index out of bounds.
}



I tried all the ways i know to resolve the path, or at least copy it to my app's private folder and then read it, but i can't overcome this issue.
When i share the .m4a file from the recorder app to Whatsapp it works, meaning that in some way i can access that file .

I tried both android.support.v4.content.FileProvider and androidx.core.content.FileProvider - nothing changed.
My filepaths.xml:
<paths>
<external-path name="external_files" path="."/>
<external-path name="files_root" path="Android/data/" />
</paths>

I also tried:
Java.IO.File f = new Java.IO.File(filePath);
var u = FileProvider.GetUriForFile(m_context, uri.Authority, f); <---- fails


Can somebody please enlighten me?
Thanks.

*Update:
When trying doing the following:
var uri = FileProvider.GetUriForFile(ApplicationContext, "com.nll.acr.provider.AttachmentProvider", new Java.IO.File(uri));
I get an exception:
Java.Lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@TomerZ-1923

You could try to get actual path from file url with dependency service .

Refer to https://forums.xamarin.com/discussion/comment/428649/#Comment_428649 .

0 Votes 0 ·

0 Answers