Reputation: 596
I am writing an app that can upload files. The files are either received by using StartActivityForResult
or received via share menu from other apps.
I have run into some problems with receiving files via share menu, specifically determining the file info. Before an upload i need to know the name and size of the file. When sharing from apps like gallery app or Google Photos, everything works fine - URI with content://
scheme is received and i can use ContentResolver
to get the name and size. But from some apps i receive a URI with file://
scheme and the same method doesn't work anymore. Is there a way to get file info from URIs with both schemes? And is there a way that guarantees that it will work for all possible URIs?
Code for getting file info:
string folderName = "";
Android.Net.Uri tempUri = null;
switch (Intent.Action) {
case Intent.ActionSend:
var result = Intent.GetParcelableExtra (Intent.ExtraStream);
tempUri = Android.Net.Uri.Parse(result.ToString());
break;
case Intent.ActionSendMultiple:
var uris = Intent.GetParcelableArrayListExtra(Intent.ExtraStream);
tempUri = Android.Net.Uri.Parse(uris[0].ToString());
break;
}
if (tempUri != null) {
var cursor = this.ContentResolver.Query (tempUri, null, null, null, null);
if (cursor != null && cursor.MoveToFirst ())
folderName = cursor.GetString (cursor.GetColumnIndex (Android.Provider.OpenableColumns.DisplayName));
}
Upvotes: 0
Views: 123
Reputation: 1006924
Is there a way to get file info from URIs with both schemes?
Wrap the file:
or content:
Uri
in a DocumentFile
. Then, call getType()
and length()
on the DocumentFile
. In the case of file:
and getType()
, I presume that DocumentFile
is using MimeTypeMap
under the covers to try to guess a MIME type.
And is there a way that guarantees that it will work for all possible URIs?
No.
Upvotes: 1