7 package com.google.appinventor.components.runtime;
9 import android.content.Intent;
11 import android.net.Uri;
13 import android.webkit.MimeTypeMap;
46 @DesignerComponent(version = YaVersion.SHARING_COMPONENT_VERSION,
47 description =
"Sharing is a non-visible component that enables sharing files and/or " +
48 "messages between your app and other apps installed on a device. The component " +
49 "will display a list of the installed apps that can handle the information provided, " +
50 "and will allow the user to choose one to share the content with, for instance a " +
51 "mail app, a social network app, a texting app, and so on.<br>" +
52 "The file path can be taken directly from other components such as the Camera or the " +
53 "ImagePicker, but can also be specified directly to read from storage. Be aware that " +
54 "different devices treat storage differently, so a few things to try if, " +
55 "for instance, you have a file called arrow.gif in the folder " +
56 "<code>Appinventor/assets</code>, would be: <ul>" +
57 "<li><code>\"file:///sdcard/Appinventor/assets/arrow.gif\"</code></li> or " +
58 "<li><code>\"/storage/Appinventor/assets/arrow.gif\"</code></li></ul>",
59 category = ComponentCategory.SOCIAL,
60 nonVisible =
true, iconName =
"images/sharing.png")
62 @UsesPermissions(permissionNames =
"android.permission.READ_EXTERNAL_STORAGE")
66 super(container.
$form());
73 @
SimpleFunction(description =
"Shares a message through any capable " +
74 "application installed on the phone by displaying a list of the available apps and " +
75 "allowing the user to choose one from the list. The selected app will open with the " +
76 "message inserted on it.")
77 public
void ShareMessage(String message) {
78 Intent shareIntent =
new Intent(Intent.ACTION_SEND);
79 shareIntent.putExtra(Intent.EXTRA_TEXT, message);
80 shareIntent.setType(
"text/plain");
84 this.form.startActivity(shareIntent);
91 @
SimpleFunction(description =
"Shares a file through any capable application "
92 +
"installed on the phone by displaying a list of the available apps and allowing the " +
93 "user to choose one from the list. The selected app will open with the file inserted on it.")
94 public
void ShareFile(String file) {
95 ShareFileWithMessage(file,
"");
102 @
SimpleFunction(description =
"Shares both a file and a message through any capable application "
103 +
"installed on the phone by displaying a list of available apps and allowing the user to " +
104 " choose one from the list. The selected app will open with the file and message inserted on it.")
105 public
void ShareFileWithMessage(String file, String message) {
106 if (!file.startsWith(
"file://"))
107 file =
"file://" + file;
109 Uri uri = Uri.parse(file);
110 File imageFile =
new File(uri.getPath());
111 if (imageFile.isFile()) {
112 String fileExtension = file.substring(file.lastIndexOf(
".")+1).toLowerCase();
113 MimeTypeMap mime = MimeTypeMap.getSingleton();
114 String type = mime.getMimeTypeFromExtension(fileExtension);
117 type =
"application/octet-stream";
121 Intent shareIntent =
new Intent(Intent.ACTION_SEND);
122 shareIntent.putExtra(Intent.EXTRA_STREAM, shareableUri);
123 shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
124 shareIntent.setType(type);
125 if (message.length() > 0) {
126 shareIntent.putExtra(Intent.EXTRA_TEXT, message);
130 this.form.startActivity(shareIntent);
133 String eventName =
"ShareFile";
134 if (message.equals(
""))
135 eventName =
"ShareFileWithMessage";
136 form.dispatchErrorOccurredEvent(
Sharing.this, eventName,