Android Question concat two variable in byte

Masoud44

Member
Hi ...
Is it possible to concat two variables in bytes for php post (var name and var value).
Is it possible to use postbyte in this way? I used combine and join instead of concat, but it didn't work
post:
    Dim var As String="img="
    Dim bmp As Bitmap=ImageView1.Bitmap
    Dim i() As Byte = ImageToBytes(bmp)
    Dim vb() As Byte=var.GetBytes("UTF8")
    Dim data() As Byte = Concat(vb, i)    ' vb+i  such as & for concat two string
    Dim j As HttpJob
    j.Initialize("insertimg", Me) 'jobdon
    j.PostBytes(ServerIP & "insert.php", data)
concat:
Sub concat(vname as byte,image as byte) as byte
    dim c as byte
    c=vname+image ??????????
    return c
End Sub
 
Solution
I congratulate you very well.
Please upload it along with the php code.
B4X
B4X:
    Dim job As HttpJob
    job.Initialize("", Me)
    job.PostBytes("http://localhost/ch01/test.php", concat("img=".GetBytes("utf8"),File.ReadBytes(File.DirAssets,"image.jpg")))
PHP
PHP:
<?php

$rawPostData = file_get_contents('php://input');

$byteArray = substr($rawPostData,4);

file_put_contents("image.jpg",$byteArray);

echo "Received byte data length: " . strlen((String)$byteArray);
?>

teddybear

Well-Known Member
Licensed User
Hi ...
Is it possible to concat two variables in bytes for php post (var name and var value).
Is it possible to use postbyte in this way? I used combine and join instead of concat, but it didn't work
concat:
Sub concat(vname as byte,image as byte) as byte
    dim c as byte
    c=vname+image ??????????
    return c
End Sub
The vname and image should be byte array and it should return a byte array in sub concat
Do concat using ByteConverter library.
B4X:
Sub concat(vname() As Byte, image() As Byte) As Byte()
    Dim c(vname.Length+image.Length) As Byte
    Dim bc As ByteConverter
    bc.ArrayCopy(vname, 0, c, 0, vname.Length)
    bc.ArrayCopy(image,0, c, vname.Length, image.Length)
    Return c  
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is it possible to concat two variables in bytes for php post (var name and var value).
Is it possible to use postbyte in this way? I used combine and join instead of concat, but it didn't work
What are you doing on the serverside with these concatenation? You can not split them afterwards.

What exactly are you trying to archieve?
 
Upvote 0

Masoud44

Member
The vname and image should be byte array and it should return a byte array in sub concat
Do concat using ByteConverter library.
B4X:
Sub concat(vname() As Byte, image() As Byte) As Byte()
    Dim c(vname.Length+image.Length) As Byte
    Dim bc As ByteConverter
    bc.ArrayCopy(vname, 0, c, 0, vname.Length)
    bc.ArrayCopy(image,0, c, vname.Length, image.Length)
    Return c 
End Sub
thank you for answer.
I'm check it
 
Upvote 0

Masoud44

Member
What are you doing on the serverside with these concatenation? You can not split them afterwards.
I hope that PHP itself will do this with the post command, and if it doesn't, I will try to it myself.
this is done in the code below, and in this case, PHP separates the variable name from the variable content with the post command
postbyte:
    Dim data As String
    Dim imageStr As String = BitmapToString(image)   
    data="img="&imageStr
    Dim j As HttpJob
    j.Initialize("insertimg", Me)    
    j.postbyte(ServerIP & "insert.php",data.GetBytes("UTF8"))

What exactly are you trying to archieve?
I want to keep the smallest possible size of an image in the database, which means about thirty percent less than what happens in the above code.
 
Upvote 0

Masoud44

Member
The vname and image should be byte array and it should return a byte array in sub concat
Do concat using ByteConverter library.
B4X:
Sub concat(vname() As Byte, image() As Byte) As Byte()
    Dim c(vname.Length+image.Length) As Byte
    Dim bc As ByteConverter
    bc.ArrayCopy(vname, 0, c, 0, vname.Length)
    bc.ArrayCopy(image,0, c, vname.Length, image.Length)
    Return c 
End Sub
This code is the same as the combine code:
CombineBytes:
Sub CombineBytes(arr1() As Byte, arr2() As Byte) As Byte()
    Dim res(arr1.Length + arr2.Length) As Byte
    Dim bc As ByteConverter 'ByteConverter library
    bc.ArrayCopy(arr1, 0, res, 0, arr1.Length)
    bc.ArrayCopy(arr2, 0, res, arr1.Length, arr2.Length)
    Return res
End Sub

The post command in PHP recognizes the name of the variable, but the content of the variable is not the image byte.
 

Attachments

  • byteimage.png
    byteimage.png
    2.8 KB · Views: 23
Upvote 0

teddybear

Well-Known Member
Licensed User
postbyte:
    Dim data As String
    Dim imageStr As String = BitmapToString(image)  
    data="img="&imageStr
    Dim j As HttpJob
    j.Initialize("insertimg", Me)   
    j.postbyte(ServerIP & "insert.php",data.GetBytes("UTF8"))


I want to keep the smallest possible size of an image in the database, which means about thirty percent less than what happens in the above code.
It does EncodeBase64 in BitmapToString(image), do you do DecodeBase64 for this?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
The post command in PHP recognizes the name of the variable, but the content of the variable is not the image byte.
This is the question I asked on #post 7. what is the content of the variable? bytes of encodebase64 ?
 
Upvote 0

Masoud44

Member
This is the question I asked on #post 7. what is the content of the variable? bytes of encodebase64 ?
lets me to say this way, when I use encodebase64 and decodebase64, there is no problem, but at in this way the size of the file increases, as a result, the speed of the application decreases. For this reason, I am trying to save the images in the database without using encode to occupy 30% less space in the database to increase the speed of reading and writing images.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
lets me to say this way, when I use encodebase64 and decodebase64, there is no problem, but at in this way the size of the file increases, as a result, the speed of the application decreases. For this reason, I am trying to save the images in the database without using encode to occupy 30% less space in the database to increase the speed of reading and writing images.
base64 adds 30% to it. Save the BYTES instead.

Honestly storing Images in the Database is a very BAD databasedesign.
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
I have tested the sub concat with PHP server, it works fine.
As DonManfred said, storing Image to database is not good choice.

 
Upvote 0

Masoud44

Member
See #13. Use Postmustipart. Your php must be able to handle it though.

Away from that the forumsearch IS working. Just use it!
Yes, I had seen and followed post #13, but I think it was related to uploading images to the server and saving the addresses of the files in the database.
If it is not like this, then I did not understand it.
Anyway, thank you for taking the time to answer.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I congratulate you very well.
Please upload it along with the php code.
B4X
B4X:
    Dim job As HttpJob
    job.Initialize("", Me)
    job.PostBytes("http://localhost/ch01/test.php", concat("img=".GetBytes("utf8"),File.ReadBytes(File.DirAssets,"image.jpg")))
PHP
PHP:
<?php

$rawPostData = file_get_contents('php://input');

$byteArray = substr($rawPostData,4);

file_put_contents("image.jpg",$byteArray);

echo "Received byte data length: " . strlen((String)$byteArray);
?>
 
Upvote 0
Solution
Top