Share bitmap question (how to)

pluton

Active Member
Licensed User
Longtime User
Hi
I know how to share a text:

B4X:
mytext = "some text"

Sub share_Click

   share.Initialize(share.ACTION_SEND,"")
   share.SetType("text/plain")
   share.PutExtra("android.intent.extra.TEXT", mytext)
   share.WrapAsIntentChooser("Share text:)")
   StartActivity(share)
   
End Sub

But how I can share a bitmap. I have tried:
B4X:
Sub share_Click

   share.Initialize(share.ACTION_SEND,"")
   share.SetType("image/jpeg")
   share.PutExtra("android.intent.extra.stream", mypicture)
   share.WrapAsIntentChooser("Share Photo:)")
   StartActivity(share)
   
End Sub
 

pluton

Active Member
Licensed User
Longtime User
I have just tried but it doesn't work. It doesn't give any error. Just wont add picture as atachment in the list gmail, facebook or twitter etc....

I put pic in root on sdcard

And tried:
B4X:
Dim mypic As Bitmap
mypic = LoadBitmap(File.DirDefaultExternal, "photo.jpg")
   
   share.Initialize(share.ACTION_SEND,"")
   share.SetType("image/jpeg")
   share.PutExtra("android.intent.extra.stream", mypic)
   share.WrapAsIntentChooser("Share Photo:)")
   StartActivity(share)

I have also tried:

mypic.Initialize(File.DirDefaultExternal, "photo.jpg")

and then call mypic but it doesn't help also :(
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
No it doesn't work

Here is part of my code so i just put image in my app

Dim fotka As Bitmap
fotka = LoadBitmap(File.DirAssets,"new_photo.jpg")

B4X:
Sub sharex_Click

   Dim shareout As OutputStream
   shareout = File.OpenOutput(File.DirRootExternal, "photo.jpg", False)
   fotka.WriteToStream(shareout,100,"JPEG")
   shareout.Close
   ToastMessageShow("Saved to root folder.",False)
   [COLOR="SeaGreen"]' It saves image on SD card. I tested it[/COLOR]
   
   share.Initialize(share.ACTION_SEND,"")
   share.SetType("image/jpeg")
   share.PutExtra("android.intent.extra.stream", "file:///sdcard/photo.jpg")
   share.WrapAsIntentChooser("Share Photo:)")
   StartActivity(share)
[COLOR="SeaGreen"]' This part doesn't put image in mail or facebook app, or messaging, or twitter...[/COLOR]
   
End Sub

Erel can you test it where I go wrong.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works with Gmail:
B4X:
   Dim r As Reflector
   Dim f As Object
   f = r.CreateObject2("java.io.File", Array As Object("file:///sdcard/1.jpg"), Array As String("java.lang.String"))
   Dim share As Intent
   share.Initialize(share.ACTION_SEND,"")
   share.SetType("image/jpeg")
   share.PutExtra("android.intent.extra.STREAM", r.RunStaticMethod("android.net.Uri", "fromFile", _
      Array As Object(f), Array As String("java.io.File")))
   share.WrapAsIntentChooser("Share Photo:)")
   StartActivity(share)
I do not know if other apps support this exact format.
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
That code i have tried now and it works for Gmail (image 1) but doesn't work for others.

I will play it with this next week.
I have added screenshots for applications which I have tried and it only work on Gmail.

Image 0 (my list of apps)............................................. ...........Image 1 (Gmail app work)

14dproo.png
........
29p9mqx.png



Image 2 (tried with Facebook)............................................. ......Image 3 (tried with Twitter)

8zpkrr.png
........
120mdue.png


Image 4 (tried with tweet caster)

9i8vn8.png


If anyone find out how let me know :sign0163:
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi All,

Sorry to bring this out of the grave,, tried searching the forum, but could not get any answers.

Any progress on how to share images to other apps via intents?

Is this a limitation in B4A? If yes, then maybe a library needs to be developed for this, as other apps can share images quite easily with the intents chooser thingy.

Cheers!
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

The Code you have given above Works for single image.

Is there a way to make it work for multiple images?

Regards,
SK
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   ShareMultipleImages(Array As String(File.Combine(File.DirRootExternal, "bitmap.png"), _
     File.Combine(File.DirRootExternal, "1.jpg")))
End Sub

Sub ShareMultipleImages(files As List)
   Dim i As Intent
   i.Initialize("android.intent.action.SEND_MULTIPLE", "")
   i.SetType("image/jpeg")
   Dim Uris As List
   Uris.Initialize
   For Each f As String In files
     Dim u As Uri
     u.Parse("file://" & f)
     Uris.Add(u)
   Next
   Dim jo As JavaObject = i
   jo.RunMethod("putParcelableArrayListExtra", Array As Object("android.intent.extra.STREAM", Uris))
   StartActivity(i)
End Sub

Depends on JavaObject and ContentResolver libraries.
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Erel.......You are a Wizard..!!! :) :) works brilliantly :)

Applications Tested :

WhatsApp
Gmail
Picassa
Facebook Messenger
Google Keep
Google Drive

Cheers :) :D
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

B4X:
i.WrapAsIntentChooser("Share Photo:)")
Does not work with the above multiple sharing code block. Is there a way to add it.?

Regards,
SK
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
it compiles with no error but when i click on the share button, all the supported application appears in the window if u click on anyone the respective app just opens(for keep it opens a new note with no image) .

Also i observed is when u don't include the line that and when i click on share and select the respective app, you get an option like just once or always. This will not appear when u include WrapAsIntentChooser.
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
hi erel,

i've tried your code and it works fine with gmail.
also i can share with text only and it works.

is it possible to share text and image together?
 
Upvote 0
Top