Android Question Open and Save a PDF File In the Database with App.

WebQuest

Active Member
Licensed User
Hi I'm trying to select a pdf file in the device with the Chooser as it happens with the images, but without success. My intent is to select a pdf through my app and then save it in the db of the app. I'm using SqlLite for the db. I'm using the chooser specifying the type of file to be selected but the pdfs on the device are not selectable, maybe you miss the file type header? And I was wondering if you need to use the BLOB to save it in the db? Thanks to the availability.


B4X:
Sub Process_Globals

Dim Chooser As ContentChooser
Dim pdf As PDFRenderer

End Sub

Sub ImageViewAggPDF_Click

    Chooser.Initialize("chooser")
    Chooser.Show("pdf/*","Select an Pdf")
    ToastMessageShow("Select PDF",True)

End Sub


Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
 
    If Success Then
     
    pdf.Initialize(File.DirRootExternal,Dir,FileName)

    ImageViewResult.Bitmap=pdf.renderPageforDisplay(1)
 
 
    Else
        ToastMessageShow("No PDF SELECT",True)
    End If
 
 
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
 
    If Success Then
    
        Dim data() As Byte = File.ReadBytes(Dir, FileName)
        SQL.ExecNonQuery2("INSERT INTO table1 VALUES(?)", Array(data))
 
    Else
        ToastMessageShow("No PDF SELECT",True)
    End If
 
 
End Sub
 
Upvote 0

WebQuest

Active Member
Licensed User
Thanks Erel, which library should I add for the (ReadBytes)?
The problem is that when I open the Chooser the pdf files are not selectable.
 
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
Guys anyone know how to set the Mimi type of the Chooser to make the PDF files selectable on the device?
 
Upvote 0

WebQuest

Active Member
Licensed User
Thanks, Erel very clear, I'm having problems with the chooser, it seems that even setting the type of file to look for (Pdf) does not work files on my device displays them but are not selectable. Where am I doing wrong?
B4X:
Sub ImageViewAggPDF_Click

    Chooser.Initialize("Chooser")
    Chooser.Show("pdf/*","Select an Pdf")
    ToastMessageShow("Select PDF",True)

End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Show has Mime as the first argument. "pdf/*" is not a mimetype.
Try "application/pdf"
 
Upvote 0

WebQuest

Active Member
Licensed User
Thanks DonManfred Solved it was so simple! Now I'm trying to load with the method AddTwoLinesAndBitmap all the pages of the pdf file in a listview but I can not increase the i in the for loop, where am I wrong?
B4X:
Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
     
       
     If Success Then
       
        File.Copy(Dir,FileName,File.DirInternalCache,"PdfFile.pdf")
        pdf.Initialize("",File.DirInternalCache,"PdfFile.pdf")
       
       Dim ImagePage As ItemSet 'Variable Type
       
       For i = 0 To pdf.PageCount -1
       
             ImagePage.Picture= pdf.renderPageforDisplay(i)
             ListViewCountPage.AddTwoLinesAndBitmap2("","",ImagePage.Picture,ImagePage)
       
       Next
    Else
        ToastMessageShow("No PDF SELECT",True)
    End If
 
 
End Sub
 
Upvote 0

WebQuest

Active Member
Licensed User
We apologize for the complete global variable code.


is a type variable (String, String, Picture) for the listview with the AddTwolinesAndBitmap method
B4X:
Sub Globals

Type ItemSet (FristRow As String,ScondRow As String, Picture As Bitmap)

End Sub



Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
   
     
     If Success Then
     
        File.Copy(Dir,FileName,File.DirInternalCache,"PdfFile.pdf")
        pdf.Initialize("",File.DirInternalCache,"PdfFile.pdf")
     
       Dim ImagePage As ItemSet 'Variable Type
     
       For i = 0 To pdf.PageCount -1
     
             ImagePage.Picture= pdf.renderPageforDisplay(i)
             ListViewCountPage.AddTwoLinesAndBitmap2("","",ImagePage.Picture,ImagePage)
     
       Next
    Else
        ToastMessageShow("No PDF SELECT",True)
    End If
 
 
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub chooser_Result ( Success As Boolean, Dir As String, FileName As String)
 
   
     If Success Then
   
        File.Copy(Dir,FileName,File.DirInternalCache,"PdfFile.pdf")
        pdf.Initialize("",File.DirInternalCache,"PdfFile.pdf")
   
   
       For i = 0 To pdf.PageCount -1
             Dim ImagePage As ItemSet 'Variable Type (Dim inside loop)
            ImagePage.initialize   
             ImagePage.Picture= pdf.renderPageforDisplay(i)
             ListViewCountPage.AddTwoLinesAndBitmap2("","",ImagePage.Picture,ImagePage)
   
       Next
    Else
        ToastMessageShow("No PDF SELECT",True)
    End If
 
 
End Sub

PD: It is not a good idea to use thumbnails like this. You´ll ran into OOM errors fast i guess.
 
Upvote 0

WebQuest

Active Member
Licensed User
The idea is to create a view as it happens with the images, the thumbnails are resizable and adaptable to the screen, I do not understand why it does not work so not from errors, but does not load the images in the listview. I try to add pages one at a time.
 
Upvote 0
Top