Skip to content

Commit

Permalink
fix(android): make Share properly share file urls (#2338)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jan 15, 2020
1 parent 72d07e0 commit 9226d77
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.getcapacitor.plugin;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.webkit.MimeTypeMap;

import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

import java.io.File;

@NativePlugin()
public class Share extends Plugin {

Expand All @@ -21,19 +26,24 @@ public void share(PluginCall call) {
call.error("Must provide a URL or Message");
return;
}

Intent intent = new Intent(Intent.ACTION_SEND);
// If they supplied both fields, concat em
if (text != null && url != null) {
if (text != null && url != null && url.startsWith("http")) {
text = text + " " + url;
intent.setTypeAndNormalize("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
} else if(url != null) {
text = url;
if (url.startsWith("file:")) {
String type = getMimeType(url);
intent.setType(type);
Uri fileUrl = FileProvider.getUriForFile(getActivity(), getContext().getPackageName() + ".fileprovider", new File(Uri.parse(url).getPath()));
intent.putExtra(Intent.EXTRA_STREAM, fileUrl);
} else {
call.error("Unsupported url");
return;
}
}

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setTypeAndNormalize("text/plain");

intent.putExtra(Intent.EXTRA_TEXT, text);

if (title != null) {
intent.putExtra(Intent.EXTRA_SUBJECT, title);
}
Expand All @@ -44,4 +54,13 @@ public void share(PluginCall call) {
getActivity().startActivity(chooser);
call.success();
}

private String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
}

0 comments on commit 9226d77

Please sign in to comment.