Android Question ContentChooser: Why this fail in some devices.

Sub7

Active Member
Licensed User
Longtime User
I am receiving feedbacks that several devices have problems while executing this code:


B4X:
Sub Process_Globals
   Dim ImageChooser As ContentChooser
End Sub

Sub btnUp_Click
Dim q As Int
q= Msgbox2("Upload?","Upload a new picture?","Choose","","Cancel", Null)
If q = DialogResponse.POSITIVE Then

   If busyService.serviceStarted = 0 Then
   StartService(busyService)
   End If

   ImageChooser.show("image/*", "Choose image")
   Else If q = DialogResponse.NEGATIVE Then
     ToastMessageShow("Operation cancelled", True)
   Else If q = DialogResponse.CANCEL Then
     ToastMessageShow("Operation cancelled", True)
   End If

End Sub


Sub imgChooser_Result(Success As Boolean, dir As String, FileName As String)

  If Success Then
     Try
RandFileName = RandomString(70,True,True,True) & "-" & Rnd(0001,9999) &"-"& Rnd(3350,8721) &"-"& Rnd(1223,3321) & "-" & static_functions.phone_ID & ".jpg"

     File.Copy(dir,FileName,File.DirInternal&"/MYFOLDER/",RandFileName)
     FitCenterBitmap(profile_picture, RandFileName)
    StopService(busyService)
    Catch
    Log(LastException.Message)
    End Try
 
    Else
     ToastMessageShow("There was an errror while processing your image, please try again", True)
   End If

End Sub



Sub FitCenterBitmap(Imv As ImageView, FileName As String)

   Private bmp As Bitmap = LoadBitmapSample(File.DirInternal&"/MYFOLDER/", FileName,200dip,200dip)
  Private cvs As Canvas
  cvs.Initialize(Imv)

  Dim rectDest As Rect
  Dim delta As Int
  If bmp.Width / bmp.Height > Imv.Width / Imv.Height Then
  delta = (Imv.Height - bmp.Height / bmp.Width * Imv.Width) / 2
  rectDest.Initialize(0, delta,Imv.Width, Imv.Height - delta)
  Else
  delta = (Imv.Width - bmp.Width / bmp.Height * Imv.Height) / 2
  rectDest.Initialize(delta, 0, Imv.Width - delta, Imv.Height)
  End If

   cvs.DrawColor(Colors.Black)
  cvs.DrawBitmap(bmp, Null, rectDest)

    Dim out As OutputStream
    out=File.OpenOutput(File.DirInternal&"/MYFOLDER/", FileName, False)
      bmp.WriteToStream(out,100,"JPEG")
  out.close

  Imv.Invalidate
    'picChanged = 1
    
End Sub


Manifest:

B4X:
  <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="14"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

This works on all my samsung devices, but.. :€
 

Sub7

Active Member
Licensed User
Longtime User
Hard to say without more information about the problem. On some (usually older) devices the process might be killed when it is in the background and then the result will be lost.

The solution is to create a service and call Service.StartForeground before you call ImageChooser.Show.

I did Erel:
B4X:
If busyService.serviceStarted = 0 Then
StartService(busyService)
End If

Is there anything wrong with rest of my code? could be possible that copy fails for some reason?
I have tried this code with different devices and no problems at all, i think must write all exceptions on file to check what's going on.
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
You know, people just say it doesn't works, i will write a file and check what happen.
If this code have no problems then i must think some devices just works in their way.. ?? :€

thx
 
Upvote 0
Top