Android Question Share an image taken from the database.

WebQuest

Active Member
Licensed User
I'm trying to share an image extracted from the database, but I keep getting this error : java.lang.RuntimeException: java.lang.IllegalArgumentException: method java.io.File.<init> argument 1 has type java.lang.String, got android.graphics.Bitmap


I'm using this procedure to recover the image from the db. The variable (ShareBitmap) inherits the image from the db.


B4X:
Sub ListViewImage_ItemLongClick (Position As Int, Value As Object)
    PanelOpzioniPass.Visible=True
  
    Dim Cursor1 As Cursor
    Dim Name As String
    Dim Buffer() As Byte
    Dim IpSt As InputStream
    Dim Bitmap1 As Bitmap
  
  

    Cursor1 = s.ExecQuery("SELECT  Picture FROM Image")

  
    For i = 0 To ListViewImage.Size-1
        Cursor1.Position = Position
      
    Next
    Buffer = Cursor1.GetBlob("Picture")
    IpSt.InitializeFromBytesArray(Buffer,0, Buffer.Length)
    IpSt.Close
    Bitmap1.Initialize2(IpSt)
    ShareBitmap = Bitmap1
    Cursor1.Close
          
 
 End Sub



through the Share button (Share) I create the event, with the Variable (ShareBitmap) that contains the image.
B4X:
Sub BtCondividi_Click
    Dim r As Reflector
    Dim f As Object
    f = r.CreateObject2("java.io.File",  Array As Object(ShareBitmap), Array As String("java.lang.String"))
    Dim share As Intent
    share.Initialize(share.ACTION_SEND,"")
    share.SetType("image/*")
    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 With")
    StartActivity(share)

End Sub
 

MarkusR

Well-Known Member
Licensed User
Longtime User
make a breakpoint start in debug mode and look if the sharebitmap is valid.

i would write it in this order, close after used
B4X:
Bitmap1.Initialize2(IpSt)
IpSt.Close

and shorter
B4X:
ShareBitmap.Initialize2(IpSt)

this i used to share a db file
B4X:
Sub Upload
 
    Dim DB1 As DB
    DB1.Initialize
 
    Dim i As Intent
    i.Initialize(i.ACTION_SEND, "")
    i.SetType("*/*")
    Dim jo As JavaObject = i
    Dim uri1 As Uri 'ContentResolver library
    uri1.Parse("file://" & DB1.FileName )
    jo.RunMethod("putExtra", Array("android.intent.extra.STREAM", uri1)) 'put a parcelable object
    i.PutExtra("android.intent.extra.SUBJECT", "My Database")
    StartActivity(i)
 
End Sub

B4X:
Sub FileName As String
  
    Dim rtp As RuntimePermissions
  
    Return File.Combine(rtp.GetSafeDirDefaultExternal("db"),  "Inspections.db")
  
End Sub
 
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
Thanks for the reply, but since I use a listview of images (i) and (LongClick) on the (item) it opens a panel with options. With your example I can not select the desired image from the listview. There is another way to share the value of a variable (BItmap)?.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm trying to share an image extracted from the database, but I keep getting this error : java.lang.RuntimeException: java.lang.IllegalArgumentException: method java.io.File.<init> argument 1 has type java.lang.String, got android.graphics.Bitmap
Please post the full error message from the logs.

Note that your file sharing code will not work on new versions of Android. You should use FileProvider from Android 7+ (API level 24+).
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@Erel
is that example better / up to date?

B4X:
Sub Upload
   
    'see also Manifest Editor!

    Dim DB1 As DB 'own class for handle the database
    DB1.Initialize
   
    Dim in As Intent
    in.Initialize(in.ACTION_SEND, "")
    in.SetType("application/*") 'it is not related to the file itself.
    in.PutExtra("android.intent.extra.STREAM", Tool.CreateFileProviderUri(DB1.Path,DB1.FileName))
    in.PutExtra("android.intent.extra.SUBJECT", "My Database")
    in.Flags = 1
    StartActivity(in)
   
End Sub

B4X:
Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

Manifest Editor
B4X:
'Share (Send the) Database File
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)

'The Used Path
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="db" />
)
 
Upvote 0

WebQuest

Active Member
Licensed User
Maybe I did not express myself well I need to share a bitmap image in an external app ... Ex. Facebook, twitter, gmail, instagram, etc ...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe I did not express myself well I need to share a bitmap image in an external app ... Ex. Facebook, twitter, gmail, instagram, etc ...

I'm using this procedure to recover the image from the db. The variable (ShareBitmap) inherits the image from the db.
Save the image to disc. Share the file written with FileProvider using the right path.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…