Android Question Base64 to PDF problem

Juanll2003uy

Member
Licensed User
Hi i have a problem with this procedure, i pass a correct base64 code but the pdf generated is 0kb

Attached is the correct base64 string that i send by parameter

cBase64:
Sub cBase64 ( str As String)
    Dim B64 As Base64
    Dim outs As OutputStream
    Dim I As Intent
    Dim ss As String
    Dim FileName As String = "dataP.pdf"
    File.Delete(File.DirDefaultExternal, "dataP.pdf")
    outs = File.OpenOutput(File.DirDefaultExternal, "dataP.pdf", False)
    outs.WriteBytes(B64.DecodeStoB( str ) , 0 , B64.DecodeStoB( str ).Length )
    
    Try
        'Dim Intent1 As Intent
        'Dim pm As PackageManager
        'Intent1 = pm.GetApplicationIntent("com.asus.filemanager")
        'StartActivity (Intent1)
        Dim i As Intent
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirDefaultExternal, "dataP.pdf"))
        i.SetType("application/pdf")
        StartActivity(i)
    Catch
        ToastMessageShow("Error al ejecutar", True)
    End Try
    
End Sub

Any suggestion?
Thanks
 

Attachments

  • impresion.txt
    8.5 KB · Views: 22

DonManfred

Expert
Licensed User
Upvote 0

Juanll2003uy

Member
Licensed User
I tried to change to File.DirInternal but is the same, the pdf is 0kb

cBase64:
Sub cBase64 ( str As String)
    Dim B64 As Base64
    Dim outs As OutputStream
    Dim I As Intent
    Dim ss As String
    Dim FileName As String = "dataP.pdf"
    File.Delete(File.DirInternal, "dataP.pdf")
    outs = File.OpenOutput(File.DirInternal, "dataP.pdf", False)
    outs.WriteBytes(B64.DecodeStoB( str ) , 0 , B64.DecodeStoB( str ).Length )
    
    Try
        'Dim Intent1 As Intent
        'Dim pm As PackageManager
        'Intent1 = pm.GetApplicationIntent("com.asus.filemanager")
        'StartActivity (Intent1)
        Dim i As Intent
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternal, "dataP.pdf"))
        i.SetType("application/pdf")
        StartActivity(i)
    Catch
        ToastMessageShow("Error al ejecutar", True)
    End Try
    
End Sub
 
Upvote 0

Juanll2003uy

Member
Licensed User
I also tried with AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) but didnt work, i think is another problem not permissions,
because in the app i use File.DirDefaultExternal for another process and it works fine to save files there

The pdf generated is ever 0kb broken
 
Upvote 0

Juanll2003uy

Member
Licensed User
This is the manifest of the app, i have android 10 in my phone, everything works fine except the pdf save

AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
 
Upvote 0

OliverA

Expert
Licensed User
Are you sure you are suffering from a 0 byte PDF, not from a FileUriExposedException? Change your toast message to
B4X:
ToastMessageShow(LastException, True)
and see what you get
 
Upvote 0

Juanll2003uy

Member
Licensed User
Hi, i tried it but the ToastMessage doesnt show, it generates the pdf and when run the Intent StartActivity(i) show
"corrupted file" and if i search the pdf and is 0kb always

This code worked some time ago in an older android in a J5 i remember, but now doesnt work in my S9
 
Upvote 0

OliverA

Expert
Licensed User
I'm attaching a working B4XPages sample project (it contains working B4J and B4A examples). The differences are 1) I'm using StringUtils for the Base64 decoding and for B4A, I'm using FileProvider.
 

Attachments

  • B642PDF.zip
    31.6 KB · Views: 23
Upvote 0
Top