Android Question How can I INSERT/Save a Pic/Photo from phone galery in .db ?

Itila Tumer

Active Member
Licensed User
Longtime User
Hi
I can take a photo , also can research on galery and get into my Panel or İmageview.
But my problem is how I can insert into my sqlite database by ID. When I say ADD I want to entry
I have blob column which is 'Resim'. And I also can read from table in my persons.db


I can add text views,
Names
LastNames
And
Photo

B4X:
Sub AddEntry
    Dim Query As String
    Dim Cursor1 As Cursor
    Dim ID As Int
    'SelectedCinsiyet = spnCinsiyet.SelectedIndex
    'first we check if the entry already does exist                              DİKKAT => BİRÇOK kez  AND koymayı unuttum aralarına                                       
    Query = "SELECT * FROM persons INNER JOIN body ON persons.ID = body.IDbody WHERE persons.ID = body.IDbody AND FirstName = ? AND LastName = ? AND Sex = ? AND BirthDay = ? AND BirthCity = ? AND FatherName = ? AND Boy = ? AND Resim = ?"
    Cursor1 = Activity2.SQL1.ExecQuery2(Query, Array As String (edtFirstName.Text, edtLastName.Text, spnCinsiyet.SelectedItem, edtBirthDay.Text, edtBirthCity.Text, edtFatherName.Text, edtBoy.Text))
 
    If Cursor1.RowCount > 0 Then
        'if it exists show a message and do nothing else
        ToastMessageShow("This entry already exists", False)
    Else
        'if not, add the entry
        'a NULL for the ID column increments the primary key automatically by one
        'we use ExecNonQuery2 because it's easier, we don't need to take care of the data types
        Query = "INSERT INTO persons VALUES (NULL, ?, ?, ?, ?, ?, ?, NULL)"
        Activity2.SQL1.ExecNonQuery2(Query, Array As String(edtFirstName.Text, edtLastName.Text, spnCinsiyet.SelectedItem, edtBirthDay.Text, edtBirthCity.Text, edtFatherName.Text))
        Query = "INSERT INTO body VALUES (NULL, NULL, ?, NULL, NULL)"
        Activity2.SQL1.ExecNonQuery2(Query, Array As String(edtBoy.Text))
        'OutputStream.InitializeToBytesArray
 
    Dim OutputStream1 As OutputStream
    Dim Buffer() As Byte 'declares an empty array
    OutputStream1 = Panel2.Tag
    OutputStream1.InitializeToBytesArray(1000)
    'Panel2.SetBackgroundImage()
    Buffer = OutputStream1.ToBytesArray
        Activity2.SQL1.ExecNonQuery2("INSERT INTO persons VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL,?)",(Array As Object (Buffer)))
     
     
        ToastMessageShow("Entry added", False)    ' confirmation for the user
     
        'to display the ID of the last entry we read the max value of the ID column
        ID = Activity2.SQL1.ExecQuerySingleResult("SELECT max(ID) FROM persons")
        Activity2.RowNumber = Activity2.RowNumber + 1                    'increase the row count
        Activity2.IDList.Add(ID)                                        'add the last ID to the list
        Activity2.CurrentIndex = Activity2.IDList.Size - 1        'set the current index to the last one
        edtID.Text = ID                                        'display the last index
    End If
    Cursor1.Close                                                'close the cursor, we don't it anymore
 
    ShowButtons
End Sub
 

Attachments

  • persons.zip
    76.1 KB · Views: 329

Itila Tumer

Active Member
Licensed User
Longtime User
yes ,thanks you , but I used this source. But I get stuck. I am taking my photo to a panel as a background. .
But In that exapmle they take photo from File and certain image like.

File.OpenInput(File.DirAssets, "smiley.gif")

I hope I could intoduce my problem exactly.

specific area is:

B4X:
Dim OutputStream1 As OutputStream
    Dim Buffer() As Byte 'declares an empty array
    OutputStream1 = Panel2.Tag
    OutputStream1.InitializeToBytesArray(1000)
    'Panel2.SetBackgroundImage()
    Buffer = OutputStream1.ToBytesArray
        Activity2.SQL1.ExecNonQuery2("INSERT INTO persons VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL,?)",(Array As Object (Buffer)))

What is wrong ,I dont know.
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
B4X:
Sub cc_Result (Success As Boolean, Dir As String, FileName As String)
    Log("Success = " & Success & ", Dir = " & Dir & ", FileName = " & FileName)
    Dim realPath As String = GetPathFromContentResult(FileName)
    Log("realPath = " & realPath)
    Panel2.SetBackgroundImage(LoadBitmapSample("", realPath, 100%x, 100%y))
End Sub
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
By the way I took this codes from another exaple which can take a image from galery.



B4X:
Sub GetPathFromContentResult(UriString As String) As String
Dim Cursor1 As Cursor
Dim cr As ContentResolver     

  If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image

  Dim Uri1 As Uri
  Dim Proj() As String = Array As String("_data")

  cr.Initialize("")
  If UriString.StartsWith("content://com.android.providers.media.documents") Then
    Dim i As Int = UriString.IndexOf("%3A")
    Dim id As String = UriString.SubString(i + 3)
    Uri1.Parse("content://media/external/images/media")
    Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
  Else
    Uri1.Parse(UriString)
    Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
  End If
 
    For i=0 To Cursor1.RowCount-1
  Cursor1.Position = 0
       
  Dim res As String
  res = Cursor1.GetString("_data")
  Next
  Cursor1.Close
  Return res
End Sub
 
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
B4X:
Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput("",realPath)
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray


I tried but I am not sure.

Or
İf I use this


B4X:
 Dim InputStream1 As InputStream
    Dim OutputStream1 As OutputStream
    Dim Buffer() As Byte 'declares an empty array
    InputStream1 = File.OpenInput(File.DirAssets,realPath)

    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)

    Buffer = OutputStream1.ToBytesArray

And I get this Error ALSO :(

I never did before , so that It makes me force a lots
 

Attachments

  • dd.PNG
    dd.PNG
    89.6 KB · Views: 296
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Italia .. this example saves a camera picture direct to your db .. Its late here . I will tidy it up tomorrow and will include getting image from gallery and saving to db .. I have given it a quick test and all seems ok . but thats it for the night ...

Cheers and goodnight
 

Attachments

  • Pictures2DBExample.zip
    8 KB · Views: 419
Last edited:
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Italia .. this example saves a camera picture direct to your db .. Its late here . I will tidy it up tomorrow and will include getting image from gallery and saving to db .. I have given it a quick test and all seems ok . but thats it for the night ...

Cheers and goodnight

Did you already have an example with getting an image of the gallery too?

I need to chose between the gallery or to take a picture.

Best regards,
André
 
Upvote 0

SCIS

Active Member
Licensed User
Longtime User
So I managed to get the camera, but how do you set it to portrait, instead of landscape?
Also, how do you fix the resolution of your camera? When I rotate the screen the view gets distorted.
 
Upvote 0
Top