Android Question Open downloaded files

Bernaert Dominique

Member
Licensed User
Longtime User
Hello,

We have a rest service that allows downloading files from our server linked to projects.
The files can be any type of file.
I have the following code (b4x project) to open the downloaded files in B4a and B4i.

B4X:
Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        #If B4A
             Dim su As StringUtils
          Dim bytes() As Byte = su.DecodeBase64(j.GetString)   
          Dim out As OutputStream = File.OpenOutput(Provider.SharedFolder, FileToOpen, False)
          
          out.WriteBytes(bytes, 0, bytes.Length)
          out.Close       
        
          LoadingIndicator.Hide
          
          Dim in As Intent
          in.Initialize(in.ACTION_SEND, "")
        
          in.SetType(MimeType)
          in.PutExtra("android.intent.extra.STREAM", Provider.GetFileUri(FileToOpen))
          in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
          StartActivity(in)
        #End If
        
        #If B4i
             Dim su As StringUtils
          Dim bytes() As Byte = su.DecodeBase64(j.GetString)   
          Dim out As OutputStream = File.OpenOutput(File.DirTemp, FileToOpen, False)
          
          out.WriteBytes(bytes, 0, bytes.Length)
          out.Close
          
          LoadingIndicator.Hide

          #If B4i
          'Dim avc As ActivityViewController
          'avc.Initialize("avc", Array(CreateFileUrl(File.DirTemp, FileToOpen)))
          'avc.Show(B4XPages.GetNativeParent(B4XPages.GetManager.GetTopPage.B4XPage), B4XPages.GetNativeParent(B4XPages.GetManager.GetTopPage.B4XPage).RootPanel)
                  
          di.Initialize("di", File.DirTemp, FileToOpen)
          di.OpenFile(B4XPages.GetNativeParent(B4XPages.GetManager.GetTopPage.B4XPage).RootPanel)
          #End if
          
          'Dim in As Intent
          'in.Initialize(in.ACTION_SEND, "")
        
          'in.SetType(MimeType)
          'in.PutExtra("android.intent.extra.STREAM", Provider.GetFileUri(FileToOpen))
          'in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
          'StartActivity(in)
        #End If
    Else
        j.Release
        LoadingIndicator.Hide
    End If

I tried this with downloading a PDF-document from the REST api.
Here included you can find the pop-up that is displayed when the download is opened.

Screenshot B4A.jpg


This is a screenshot when opening a PDF from other app.

Screenshot other apps.jpg



Does anybody know how to get the same result?

Thx,
Dominique
 

zed

Active Member
Licensed User
I use this and it works great
B4A:
Dim in As Intent
in.Initialize(in.ACTION_SEND, "")
in.PutExtra("android.intent.extra.TEXT", YourValue)
in.SetType("text/plain")
in.WrapAsIntentChooser("Share Via")
StartActivity(in)
 
Upvote 0
Top