Skip to content

Commit

Permalink
Added support for sharing files using share() method.
Browse files Browse the repository at this point in the history
If the first parameter is a path to an existing file, and the mimetype parameter is provided, then it will share as file instead of text.
  • Loading branch information
shannah committed Feb 4, 2023
1 parent 6150928 commit 1228f5c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
13 changes: 8 additions & 5 deletions CodenameOne/src/com/codename1/ui/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -4405,16 +4405,19 @@ public void share(String text, String image, String mimeType){
*
* <p>Since 6.0, there is native sharing support in the Javascript port using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share">navigator.share</a>
* API. Currently (2019) this is only supported on Chrome for Android, and will only work if the app is accessed over https:.</p>
*
* @param text String to share.
*
* <p>Since 8.0, you can share files using using the file path in the text parameter. The file must exist in file system storage, and
* you must define the appropriate mimeType in the mimeType parameter. E.g. {@code share("file:/.../myfile.pdf", null, "application.pdf") }</p>
*
* @param textOrPath String to share, or path to file to share.
* @param image file path to the image or null
* @param mimeType type of the image or null if no image to share
* @param mimeType type of the image or file. null if just sharing text
* @param sourceRect The source rectangle of the button that originated the share request. This is used on
* some platforms to provide a hint as to where the share dialog overlay should pop up. Particularly,
* on the iPad with iOS 8 and higher.
*/
public void share(String text, String image, String mimeType, Rectangle sourceRect){
impl.share(text, image, mimeType, sourceRect);
public void share(String textOrPath, String image, String mimeType, Rectangle sourceRect){
impl.share(textOrPath, image, mimeType, sourceRect);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7233,8 +7233,13 @@ public void share(String text, String image, String mimeType, Rectangle sourceRe
}*/
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
if(image == null){
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
if (text.startsWith("file:") && mimeType != null && new com.codename1.io.File(text).exists()) {
shareIntent.setType(mimeType);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fixAttachmentPath(text)));
} else {
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
}
}else{
shareIntent.setType(mimeType);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fixAttachmentPath(image)));
Expand Down
12 changes: 11 additions & 1 deletion Ports/iOSPort/nativeSources/IOSNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -7325,7 +7325,17 @@ void com_codename1_impl_ios_IOSNative_socialShare___java_lang_String_long_com_co
dataToShare = [NSArray arrayWithObjects:i, nil];
}
} else {
dataToShare = [NSArray arrayWithObjects:someText, nil];
BOOL shareFile = NO;
if (someText != nil && [someText hasPrefix:@"file:"]) {
NSURL* fileURL = [NSURL fileURLWithPath:[someText substringFromIndex:5]];
if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
shareFile = YES;
dataToShare = [NSArray arrayWithObjects:fileURL, nil];
}
}
if (!shareFile) {
dataToShare = [NSArray arrayWithObjects:someText, nil];
}
}

UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare
Expand Down

0 comments on commit 1228f5c

Please sign in to comment.