Android Question open wrong pdf file

Kiran Raotole

Active Member
Licensed User
I'm using CreateFileProviderUri for opening pdf file and
printing library for write in pdf.

When I'm opening pdf file its open last proccess pdf file.
this is my code
B4X:
Sub open_pdf
    If File.Exists(Starter.shared,"1.pdf") Then
        File.Delete(Starter.shared,"1.pdf")
    End If
    
    Dim font As Typeface
    Dim fontsize As Float
    fontsize = 6 / GetDeviceLayoutValues.Scale
    font = Typeface.SERIF
    Log(GetDeviceLayoutValues.Scale)
    
    Dim pdf As PdfDocument
    pdf.Initialize
    pdf.StartPage(595,482)
    Dim r As Int = 30
    r=r+0
    pdf.Canvas.DrawText("Name : Honey ",20,r,font,6 / GetDeviceLayoutValues.Scale,Colors.Blue,"LEFT")
    r=r+30
    pdf.Canvas.DrawText("add  : abcd",20,r,font,fontsize,Colors.Blue,"LEFT")
    r=r+30
    pdf.Canvas.DrawText("mob  : 123456",20,r,font,fontsize,Colors.Blue,"LEFT")
    r=r+30
    Dim Rect1 As Rect
    'Rect1.Initialize(100dip, 100dip, 200dip, 150dip)
    Rect1.Initialize(20,30,100,200)
    pdf.Canvas.DrawRect(Rect1, Colors.Gray, False,0)
    Activity.Invalidate
    
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutPut(Starter.shared,"1.pdf",True)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
    
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW,CreateFileProviderUri(Starter.shared,"1.pdf"))
    in.Flags=1
    StartActivity(in)
    
End Sub

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
What's a problem?
 

Yayou49

Active Member
Licensed User
to allow you to understand, maybe you can control 2 things:
after
B4X:
    If File.Exists(Starter.shared,"1.pdf") Then
        File.Delete(Starter.shared,"1.pdf")
    End If
add a control to see if your file is really deleted:

B4X:
    If File.Exists(Starter.shared,"1.pdf") Then
        File.Delete(Starter.shared,"1.pdf")
    End If

If File.Exists(Starter.shared,"1.pdf") Then
log("file exists")
else
log("file deleted")
end if
While creating your pdf and then createfileprovider, change the file name to 2.pdf

It can be a good start to understand ..... (my own opinion)
 
Top