Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Share: Share Documents/Files #425

Closed
jrahma opened this issue Jul 28, 2018 · 15 comments
Closed

Share: Share Documents/Files #425

jrahma opened this issue Jul 28, 2018 · 15 comments
Labels
feature-request A request for a new feature. in-progress Actively being worked on.
Milestone

Comments

@jrahma
Copy link

jrahma commented Jul 28, 2018

Hi,

Please add an option to Share Documents with a Title and URL..

For example, share PDF in Whatsapp or Google Drive or One Drive or DropBox..

Thanks,
Jassim

VS bug #735659

@mattleibow
Copy link
Contributor

This is probably related to the #129 and #416 and some other issues. The basic problem is that sharing files may require access to internal/local app files. I think #416 is to address the initial limitation.

@Redth Redth added need-more-information Need more information to investigate a bug or proposal needs-specifications Accepted feature/enhancement and needs specification. azdo-sync and removed need-more-information Need more information to investigate a bug or proposal labels Nov 26, 2018
@xamarin-release-manager xamarin-release-manager added the feature-request A request for a new feature. label Nov 28, 2018
@jamesmontemagno jamesmontemagno changed the title Data Transfer: Share Documents Share: Share Documents/Files Feb 4, 2019
@Redth
Copy link
Member

Redth commented Mar 16, 2019

1.1.0 has file sharing!

@Redth Redth closed this as completed Mar 16, 2019
@jamesmontemagno jamesmontemagno added this to the 1.1.0 milestone Mar 16, 2019
@xamarin-release-manager
Copy link
Collaborator

[VS sync] The field 'Milestone' contains the value '1.1.0' that is not in the list of supported values

@mfeingol
Copy link

mfeingol commented Mar 17, 2019

Nice to see this land! I gave it a spin on Android with code along these lines:

        string filePath = path to a PDF;
        string contentType = "application/pdf";

        ShareFile share = new ShareFile(filePath, contentType);
        ShareFileRequest request = new ShareFileRequest(Path.GetFileName(filePath), share);
        await Share.RequestAsync(request);

It worked, in that a system dialog opened with some apps as options. However, the apps were Gmail, Save to Drive, and Messages, aka generic options. If I use the more traditional approach (e.g. https://www.syncfusion.com/kb/8707/how-to-share-the-pdf-document-in-xamarin-forms-platform), I get PDF-specific options, such as "Drive PDF Viewer".

Is there some setup required before the feature will work as expected?

Thanks.

@jamesmontemagno
Copy link
Collaborator

You will need to enable the experimental features:

// Enable currently experimental features
ExperimentalFeatures.Enable(
ExperimentalFeatures.EmailAttachments,
ExperimentalFeatures.ShareFileRequest);

Under the hood it calls:

            var contentUri = Platform.GetShareableFileUri(request.File.FullPath);

            var intent = new Intent(Intent.ActionSend);
            intent.SetType(request.File.ContentType);
            intent.SetFlags(ActivityFlags.GrantReadUriPermission);
            intent.PutExtra(Intent.ExtraStream, contentUri);

            if (!string.IsNullOrEmpty(request.Title))
            {
                intent.PutExtra(Intent.ExtraTitle, request.Title);
            }

            var chooserIntent = Intent.CreateChooser(intent, request.Title ?? string.Empty);
            chooserIntent.SetFlags(ActivityFlags.ClearTop);
            chooserIntent.SetFlags(ActivityFlags.NewTask);
            Platform.AppContext.StartActivity(chooserIntent);

            return Task.CompletedTask;

Which looks pretty much identical to what is in that article, so should work.

@mfeingol
Copy link

@jamesmontemagno: I spent a little more time on this. Ultimately, it's a question of intent. :-)

My app currently does the following:

            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(contentUri, contentType);
            intent.SetFlags(Android.Content.ActivityFlags.ClearWhenTaskReset | Android.Content.ActivityFlags.NewTask | Android.Content.ActivityFlags.GrantReadUriPermission);
            Android.App.Application.Context.StartActivity(intent);

This results in the following system dialog:

image

Using the ActionSend intent as the code does above results in the following system dialog, which is indeed identical to the one from Xamarin.Essentials.Share.RequestAsync:

image

I have no basis for judging which of the two intents is preferable for an abstraction that involves sharing files. My particular use case is to launch an external viewer, and ActionView clearly works better for that. So for Xamarin Essentials, perhaps a different abstraction is needed for launching files, or perhaps some kind of selector on ShareFileRequest to specify the precise intention?

@jamesmontemagno
Copy link
Collaborator

Ahhhhh yes, this makes sense! Good call. The API is still not final so we can easily play around with this. I think we can add an intention on it. Or we can have "ShareFileRequest" and "ViewFileRequest".. @Redth

The question is... is it different in UWP and iOS at all?

@jamesmontemagno
Copy link
Collaborator

Re-opening for now since we are reving the API

@mfeingol
Copy link

UWP distinguishes between launching a file (Windows.System.Launcher.LaunchFileAsync) and sharing a file (Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI).

@jamesmontemagno jamesmontemagno modified the milestones: 1.1.0, 1.1.1 Apr 1, 2019
@xamarin-release-manager
Copy link
Collaborator

[VS sync] The field 'Milestone' contains the value '1.1.1' that is not in the list of supported values

@sarthak199526
Copy link

Hi friends for android version > 24 you need to use fileProvider class

use : Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);

InsteadOf : Uri uri = Uri.fromFile(fileImagePath);

Just Implemented On my app

Reference : https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

@mattleibow
Copy link
Contributor

@jamesmontemagno, maybe this is where we should be using the launcher to open files and the share to share? This may mean we have to extend the launcher apis

@RhomGit
Copy link

RhomGit commented Apr 27, 2019

Agreed with @mattleibow above.
I see this has been open for roughly 9 months but is slated for 1.1.1 … which itself is 22 days overdue. Just to be sure (I have clients requiring this functionality asap) does this mean that it can be expected "soon" or perhaps I should implement an alternative solution until some unknown future date?

I don't want to appear rude (I am grateful for all of the effort on this project) and I would prefer to use a Xamarin.Essentials function for opening files but its hard to make decisions without knowing a little more. Is work already underway on this?

@jamesmontemagno
Copy link
Collaborator

@RhomGit the ability to share is already integrated into 1.1.0 and we re-opened this to expand to opening the file. We are working on new features and bug fixes regularly.

@jamesmontemagno jamesmontemagno added in-progress Actively being worked on. and removed needs-specifications Accepted feature/enhancement and needs specification. labels Apr 27, 2019
jamesmontemagno added a commit that referenced this issue Jun 13, 2019
* Implement Open File in Launcher.  Fixed #601 & #425

* Add title to open file request for android launcher

* Clean FileBase and AttachmentName. Added documentation.

* Fix encoding

* Remove namesapces that aren' t needed
@jamesmontemagno jamesmontemagno modified the milestones: 1.1.1, 1.2.0 Jun 18, 2019
@jamesmontemagno
Copy link
Collaborator

Closing now as API is in 1.2.0-pre shipping soon :)

Mrnikbobjeff pushed a commit to Mrnikbobjeff/Essentials that referenced this issue Aug 28, 2019
…arin#773)

* Implement Open File in Launcher.  Fixed xamarin#601 & xamarin#425

* Add title to open file request for android launcher

* Clean FileBase and AttachmentName. Added documentation.

* Fix encoding

* Remove namesapces that aren' t needed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request A request for a new feature. in-progress Actively being worked on.
Projects
None yet
Development

No branches or pull requests

8 participants