33import android .app .Activity ;
44import android .content .Intent ;
55import android .net .Uri ;
6+ import android .os .AsyncTask ;
67import androidx .core .content .FileProvider ;
78import org .appcelerator .kroll .KrollDict ;
89import org .appcelerator .kroll .KrollFunction ;
1617import org .appcelerator .titanium .util .TiActivitySupport ;
1718import java .io .File ;
1819import java .io .FileOutputStream ;
20+ import java .io .InputStream ;
21+ import java .net .HttpURLConnection ;
22+ import java .net .URL ;
1923import java .util .HashMap ;
2024
2125public class ShareProxy implements TiActivityResultHandler {
@@ -35,7 +39,7 @@ public static void share(HashMap params, KrollModule module) {
3539 private void executeShare (HashMap params ) {
3640 try {
3741 String message = TiConvert .toString (params .get ("message" ), "" );
38- String subject = TiConvert .toString (params .get ("subject" ), "Compartilhar " );
42+ String subject = TiConvert .toString (params .get ("subject" ), "Share " );
3943 Object imageObj = params .get ("image" );
4044 Object callbackObj = params .get ("callback" );
4145
@@ -49,6 +53,35 @@ private void executeShare(HashMap params) {
4953 Log .d (TAG , "Image object type: " + (imageObj != null ? imageObj .getClass ().getName () : "null" ));
5054 Log .d (TAG , "Callback provided: " + (this .callback != null ));
5155
56+ // Check if image is a URL
57+ if (imageObj instanceof String && isUrl ((String ) imageObj )) {
58+ String imageUrl = (String ) imageObj ;
59+ Log .d (TAG , "Detected URL image: " + imageUrl );
60+
61+ // Download image asynchronously
62+ new ImageDownloadTask (message , subject ).execute (imageUrl );
63+
64+ } else {
65+ // Process normally (local file or blob)
66+ processShare (message , subject , imageObj );
67+ }
68+
69+ } catch (Exception e ) {
70+ Log .e (TAG , "FATAL ERROR while sharing: " + e .getMessage (), e );
71+
72+ // Call callback with error if available
73+ if (this .callback != null ) {
74+ fireCallback (false , "Error: " + e .getMessage ());
75+ }
76+ }
77+ }
78+
79+ private boolean isUrl (String str ) {
80+ return str != null && (str .startsWith ("http://" ) || str .startsWith ("https://" ));
81+ }
82+
83+ private void processShare (String message , String subject , Object imageObj ) {
84+ try {
5285 Intent shareIntent = new Intent (Intent .ACTION_SEND );
5386
5487 if (imageObj != null ) {
@@ -97,9 +130,8 @@ private void executeShare(HashMap params) {
97130 Log .d (TAG , "=== SHARE INITIATED ===" );
98131
99132 } catch (Exception e ) {
100- Log .e (TAG , "FATAL ERROR while sharing : " + e .getMessage (), e );
133+ Log .e (TAG , "ERROR in processShare : " + e .getMessage (), e );
101134
102- // Call callback with error if available
103135 if (this .callback != null ) {
104136 fireCallback (false , "Error: " + e .getMessage ());
105137 }
@@ -173,6 +205,12 @@ private static Uri getImageUri(Object imageObj) {
173205 fos .close ();
174206
175207 Log .d (TAG , "File created successfully. Size: " + imageFile .length () + " bytes" );
208+
209+ }
210+ // If it's a File object (already downloaded from URL)
211+ else if (imageObj instanceof File ) {
212+ imageFile = (File ) imageObj ;
213+ Log .d (TAG , "Using existing File object: " + imageFile .getAbsolutePath ());
176214 }
177215 // If it's a file path (String)
178216 else if (imageObj instanceof String ) {
@@ -281,4 +319,100 @@ else if (imageObj instanceof String) {
281319
282320 return null ;
283321 }
322+
323+ /**
324+ * AsyncTask to download image from URL in background
325+ */
326+ private class ImageDownloadTask extends AsyncTask <String , Void , File > {
327+
328+ private String message ;
329+ private String subject ;
330+ private String errorMessage ;
331+
332+ public ImageDownloadTask (String message , String subject ) {
333+ this .message = message ;
334+ this .subject = subject ;
335+ }
336+
337+ @ Override
338+ protected File doInBackground (String ... urls ) {
339+ String imageUrl = urls [0 ];
340+ HttpURLConnection connection = null ;
341+
342+ try {
343+ Log .d (TAG , "Starting image download from: " + imageUrl );
344+
345+ URL url = new URL (imageUrl );
346+ connection = (HttpURLConnection ) url .openConnection ();
347+ connection .setConnectTimeout (15000 );
348+ connection .setReadTimeout (15000 );
349+ connection .setRequestMethod ("GET" );
350+ connection .setDoInput (true );
351+ connection .connect ();
352+
353+ int responseCode = connection .getResponseCode ();
354+ Log .d (TAG , "Response code: " + responseCode );
355+
356+ if (responseCode == HttpURLConnection .HTTP_OK ) {
357+ InputStream input = connection .getInputStream ();
358+
359+ // Create temporary file
360+ File cacheDir = TiApplication .getInstance ().getCacheDir ();
361+ File imageFile = new File (cacheDir , "share_url_" + System .currentTimeMillis () + ".jpg" );
362+
363+ FileOutputStream output = new FileOutputStream (imageFile );
364+
365+ byte [] buffer = new byte [4096 ];
366+ int bytesRead ;
367+ long totalBytes = 0 ;
368+
369+ while ((bytesRead = input .read (buffer )) != -1 ) {
370+ output .write (buffer , 0 , bytesRead );
371+ totalBytes += bytesRead ;
372+ }
373+
374+ output .close ();
375+ input .close ();
376+
377+ Log .d (TAG , "✓ Image downloaded successfully. Size: " + totalBytes + " bytes" );
378+ Log .d (TAG , "Saved to: " + imageFile .getAbsolutePath ());
379+
380+ return imageFile ;
381+
382+ } else {
383+ errorMessage = "HTTP error code: " + responseCode ;
384+ Log .e (TAG , errorMessage );
385+ return null ;
386+ }
387+
388+ } catch (Exception e ) {
389+ errorMessage = "Download failed: " + e .getMessage ();
390+ Log .e (TAG , errorMessage , e );
391+ return null ;
392+
393+ } finally {
394+ if (connection != null ) {
395+ connection .disconnect ();
396+ }
397+ }
398+ }
399+
400+ @ Override
401+ protected void onPostExecute (File imageFile ) {
402+ if (imageFile != null && imageFile .exists ()) {
403+ Log .d (TAG , "Download completed, proceeding with share" );
404+ processShare (message , subject , imageFile );
405+ } else {
406+ Log .e (TAG , "Download failed, sharing text only" );
407+
408+ // If callback exists, notify about download failure
409+ if (callback != null ) {
410+ fireCallback (false , errorMessage != null ? errorMessage : "Failed to download image" );
411+ } else {
412+ // Share text only if no callback
413+ processShare (message , subject , null );
414+ }
415+ }
416+ }
417+ }
284418}
0 commit comments