Android Question Retrieve Pdf File From Database.

Hi Guys. I want to Retrieve A Pdf File I saved in a database. I saved Pdf File through following steps.


B4X:
' Selecting File Through ContentChooser
cc.Show("*/*", "Choose File")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)

    If Success Then
        'getFileInfo(FileName)
        ilbl_filename.Text = GetFileInfoByIndex("_display_name", FileName)
        
        is_filename = FileName
        is_dir = Dir
    
        is_imageextension = ilbl_filename.Text.Trim
        is_imageextension = is_imageextension.SubString2(is_imageextension.LastIndexof("."), is_imageextension.Length)
        is_imageextension = is_imageextension.SubString2(1,is_imageextension.Length)
        Log("Extension : " & is_imageextension)
    End If
    
    'Get File Name And Directory
    Sub GetFileInfoByIndex(column As String, uri As String) As String
    Dim results As String
    Dim Cur As Cursor
    Dim Uri1 As Uri
    Dim cr As ContentResolver
    cr.Initialize("")

    'if viewing by gallery
    If uri.StartsWith("content://media/") Then
          Dim i As Int = uri.LastIndexOf("/")
          Dim id As String = uri.SubString(i + 1)
          Uri1.Parse(uri)
          Cur = cr.Query(Uri1, Null, "_id = ?", Array As String(id), Null)
    Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    Else
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, Null, Null, Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    End If
    
    'Converting PDF File Into Bytes And Then Encode it Into String And Then Save As String into Database.
    
    Dim ls_byte() As Byte
    ls_byte = FileToBytes(is_dir, is_filename)
            
    Log("file name : " & is_filename)
            
    Dim ls_stringutils As StringUtils
    ls_photostring = ls_stringutils.EncodeBase64(ls_byte)
    
    Cur.Close
    
    Return results
    
End Sub

Now I don't Know How to retrieve the record?
 

DonManfred

Expert
Licensed User
Longtime User
Now I don't Know How to retrieve the record?
How is all above related to your Database? There is no single databasecall in the code.
You are just working with contentResolver to get Info about a File.

The File itself is on the Device storage; not in a Database.
 
Upvote 0
How is all above related to your Database? There is no single databasecall in the code.
You are just working with contentResolver to get Info about a File.

The File itself is on the Device storage; not in a Database.
Here I have Attached My Small Project. It can't run in Mobile. Just to See My Progam. I have written postgres query in Php.
 

Attachments

  • forum_sample.zip
    21.1 KB · Views: 12
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Just to See My Progam
- Do not expect me going over 1300 Lines of code just to find out what you are doing.
- There are several things missing in the Code
- There is no Database involved in the code
- there is no php-file involved

I´m out here. Good luck
 
Upvote 0
- Do not expect me going over 1300 Lines of code just to find out what you are doing.
- There are several things missing in the Code
- There is no Database involved in the code
- there is no php-file involved

I´m out here. Good luck
I have uploaded a PDF file to the Postgres Database Through an API written in PHP and then just called an API in B4A. I selected File through ContentChooser And then I converted File to Bytes Using Filename and Directory then I converted Bytes into a string and saved into the Postgres database. When I want to retrieve the File Which is in the string format. I have converted the string into bytes. But When I used to convert Bytes To File It Needed Filename and Directory. I don't know How to Retrieve. How to overcome this?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
But When I used to convert Bytes To File It Needed Filename and Directory
What I can think of is:
1. You need to store the original file name in the database if the file name is important.
2. If the file name is not important then just give it any name or auto generate a random file name when generating the file. You can use something based on the date time e.g 20240630_123045.pdf if it is generated on 30 June 2024 12:30:45.
 
Upvote 0
Top