Android Question Convert jpg to png

Mrjoey

Active Member
Licensed User
Longtime User
hello i would like to convert a inputstream wish is loaded from a jpg picture to png format , im using
B4X:
Dim In1 As InputStream
            In1.InitializeFromBytesArray(MP.Cover,0,MP.Cover.Length)
            Dim out As OutputStream = File.OpenOutput(File.DirInternal,"temp.png", False)
            File.Copy2(In1, out)
            out.Close
            In1.Close
mp.cover is a bytearray
and how to check if the bytearray is a jpg or png?
tnx.
 

MaFu

Well-Known Member
Licensed User
Longtime User
This does't work, because you create a simple 1:1 copy. Your new file have a png extension but internally it's a jpg file.
Try this:
B4X:
Dim in1 As InputStream
in1.InitializeFromBytesArray(MP.Cover,0,MP.Cover.Length)
Dim bmp As Bitmap
bmp.Initialize2(in1)
Dim out As OutputStream = File.OpenOutput(File.DirInternal,"temp.png", False)
bmp.WriteToStream(out,80,"PNG")
out.Close
in1.Close

To check the image format in the byte array:
png file: string "ëPNG" (hex: 89 50 4e 47) on the beginning
jpg file: begins with hex ff d8 ff
 
Upvote 0
Top