Android Question Compress and add photos to the CLV

red30

Well-Known Member
Licensed User
Longtime User
I upload a photo to СLV. I need to compress them to 1280x720 (720x1280), then reduce the average to ~ 300Kb. I do this as in the code below.

B4X:
Sub LoadZip
    ProgressDialogShow("text1")
    For i=0 To uris.Size-1
        Saveclv.Add(CreateListItemIV("ContentDir",uris.Get(i),i),0)
    Next
    ProgressDialogHide
End Sub
Sub CreateListItemIV(MyDir As String, MyFileName As String, MyIndex As Int) As Panel
    Dim p2 As Panel
    p2.Initialize("")
    Dim btm As Bitmap = LoadBitmapResize(MyDir,MyFileName,1280dip, 1280dip, True)
    If btm.Width>btm.Height Then
        p2.SetLayoutAnimated(0, 0, 0, 2*13.5%x, 13.5%y)
    Else
        p2.SetLayoutAnimated(0, 0, 0, 13.5%x, 13.5%y)
    End If
    p2.LoadLayout("Item_IV")
        If btm.Width>btm.Height Then
            If btm.Height>720 And btm.Width>1280 Then
                btm=btm.Resize(1280,720,True)
            End If
        Else
            If btm.Height>1280 And btm.Width>720 Then
                btm=btm.Resize(720,1280,True)
            End If
        End If
        File.Copy(MyDir,MyFileName,File.DirInternal,MyIndex&".jpg")
        If File.Size(File.DirInternal,File.DirInternal,MyIndex&".jpg")>300000 Then
            Dim p=90 As Int
            Do While (File.Size(File.DirInternal,MyIndex&".jpg")>300000)
                Dim Out As OutputStream
                Out = File.OpenOutput(File.DirInternal,MyIndex&".jpg", False)
                btm.WriteToStream(Out, p, "JPEG")
                Out.Close
                If p<=10 Then
                    p=1
                Else
                    p=p-10
                End If
            Loop
        End If
    ImageView2.Bitmap=btm.Resize(27%x, 27%y,True)
    Return p2
End Sub
This works slowly. Approximately one second per image. How can I make the download faster? Why at the time of loading ProgressDialogShow not displayed?
 

derez

Expert
Licensed User
Longtime User
With Agraham's jpeg library you can load a bitmap size before you load the bitmap itself:
LoadJpegSizeOnly (instream As java.io.InputStream)
Loads only the size of the image from instream setting JpegWidth and JpegHeight.
The image itself is not loaded.
After you get the size you can do the manipulation and load it once in the correct size, no need for resizing.
 
Upvote 0
Top