Upload Image Fail

HI, sorry to bether all of you but I have one problem that I don’t undestand

I want to make one app and in this app the user should be able to upload images from his gallery and make a copy in the folder of the app

The selection of the image works well but I always recieve the image as nil

I did a new code ultra simple for testing but I still having the same issue (is the code below)

the logs that I have in andoird studio are:

Corona usap64 I JavaToNativeShim.resume(runtime)
Corona usap64 I controller.requestRender()
DecorView usap64 D onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@1d61bfa[CoronaActivity]
ViewRootIm…aActivity] usap64 D windowFocusChanged hasFocus=true inTouchMode=true
MediaPlayer usap64 D create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1306)
at android.media.MediaPlayer.create(MediaPlayer.java:929)
at android.media.MediaPlayer.create(MediaPlayer.java:902)
at android.media.MediaPlayer.create(MediaPlayer.java:881)
at com.ansca.corona.CoronaActivity.getDurationOfVideo(CoronaActivity.java:2248)
at com.ansca.corona.CoronaActivity.access$600(CoronaActivity.java:43)
at com.ansca.corona.CoronaActivity$SelectMediaActivityResultHandler$1.run(CoronaActivity.java:2483)
at java.lang.Thread.run(Thread.java:923)

Corona usap64 I onComplete called
Corona usap64 I event.completed = true
Corona usap64 I photo = nil

I don’t even undestand the problem, the picker of the photo work, i choose different photos in different formats and resolutions but no idea…
someone have any clue where I should search?


local function onComplete(event)

  print("onComplete called")

  if event.completed then

      print("event.completed = true")

  else

      print("event.completed = false")

  end

  local photo = event.target

  if photo then

      print("photo is not nil")

      print("photo w,h = " .. photo.width .. "," .. photo.height)

      -- Guardar la imagen seleccionada en un directorio específico

      local baseDir = system.DocumentsDirectory

      local filePath = system.pathForFile("selectedImage.jpg", baseDir)

      local file = io.open(filePath, "wb")

     

      if file then

          local function onSaveComplete(event)

              if event.isError then

                  print("Error saving image: ", event.errorMessage)

              else

                  print("Image saved successfully")

              end

          end

         

          file:write(photo.data)

          io.close(file)

          print("Image saved to: " .. filePath)

         

          -- Llamar a la función de guardar la imagen en la galería

          media.save(filePath, baseDir, onSaveComplete)

      else

          print("Error: Could not open file for writing")

      end

  else

      print("photo = nil")

  end

end

local function pickPhoto(event)

  print("pickPhoto called")

  media.selectPhoto({

      mediaSource = media.SavedPhotosAlbum,

      listener = onComplete,

      origin = event.target.contentBounds,

      permittedArrowDirections = { "right" },

      destination = { baseDir = system.TemporaryDirectory, filename = "image.jpg", type = "image" }

  })

end

local button = display.newRect(120, 240, 80, 70)

button:addEventListener("tap", pickPhoto)

The below code worked for me in many apps … modify whatever you want and test it

local function onComplete2( event )
    local photo = event.target
    photo.x=display.contentCenterX
    photo.y=display.contentCenterY

    local G1=display.newGroup()
    G1:insert( photo )

    display.save( G1, { filename=yourFileName, baseDir=system.TemporaryDirectory, captureOffscreenArea=true, backgroundColor={1,1,1,0}, jpegQuality=0.8  } ) -- This will reduce image size as well
    
    display.remove(photo)
    photo=nil

    display.remove(G1)
    G1=nil

    if event.target~=nil then
      display.remove( event.target )
      event.target=nil
    end
end
local function camImg2_Tap()
    if media.hasSource( media.PhotoLibrary ) then
       media.selectPhoto( { mediaSource=media.PhotoLibrary, listener=onComplete2 } )
    else
       native.showAlert( "Info", "This device does not have a photo library.", { "Ok" } )
    end
end

camImg2_Tap()