Reading and Writing Files

Hello guys!!

I’m trying to get the user to select a photo of the device for their avatar, so far so good.

I used the library to have the user select the desired photo.

timer.performWithDelay( 100, 

function() media.selectPhoto( { listener = sessionComplete, mediaSource = PHOTO_FUNCTION, destination = { baseDir=system.TemporaryDirectory, filename=tmp_image_name } } ) end 

)

return true

– tmp_image_name corresponds to a random string of type efCvsdS.jpg

So far so good, the user chooses the image and it goes to the temporary directory. Even after the user selects the image, I show it on the screen

local sessionComplete = function(event)

photo = event.target

if event.completed then

toast.show(“picture selected”)

img_temp = display.newImageRect( tmp_image_name, system.TemporaryDirectory, 200, 300)

–local scale = math.max(endWidth / img_temp.width, endHeight / img_temp.height)

–img_temp:scale(scale, scale)

img_temp.x = display.contentCenterX - 170

img_temp.y = display.contentCenterY

else

toast.show(“no picture selected”)

end

end

I checked and the photo is actually in the temporary directory.

Done that, I have a user action that causes me to call my function to move the image from the temporary folder to the documents folder. So I can save this information in the database and be able to use that image later

if img_temp ~= nil then

image_name = getRandomId(8)…".jpg"

local check = copyFile(tmp_image_name, system.TemporaryDirectory, image_name, system.DocumentsDirectory, true )

toast.show( "copy result: " … check )

end

But the problem is precisely in this step of copying the image, I used the function found in the CORONA documentation. And it returns me a fatal error that causes the application to stop. (see error attached)

Please someone could help me to solve this error. I’ve tried everything and I can not

https://pastebin.com/BjszvN0d (the file used in copy image) ** error in line 88

thank you

You have mixed up your file names.  Try this

local tmp\_image\_name = "copied.jpg" local check = copyFile(image\_name, system.TemporaryDirectory, tmp\_image\_name, system.DocumentsDirectory, true )

It does not work, I’ve tried and it returns me the same error.

Were you able to identify the cause of the error on line 88? I could not.

Yes, the code is trying to read from an invalid file handle.  The error is because the function cannot find the filename you are passing in as srcName and  srcPath.  

Change your code to this and post the output…

if img\_temp then   image\_name = getRandomId(8)..".jpg"  print("src="..tmp\_image\_name, "dst="..image\_name)   local check = copyFile(tmp\_image\_name, system.TemporaryDirectory, image\_name, system.DocumentsDirectory, true )   toast.show( "copy result: " .. check ) end

Hello

The first image shows the temp_file_name that is generated randomly as soon as the user enters the screen.

The second image, after the user select the image, I used the function to get if the image with “temp_file_name” is in the temporary directory of the system, and the function returned me true (this I already knew because I can show the image on the screen From the temporary directory)

The third image shows that both the source (temp) and destination file have a valid name, so I pass to the “copyFile” function a valid file to a valid destination.

Is this a bug?

What I asked is to see what this line printed (in the console) as text not screenshots.

print("src="..tmp\_image\_name, "dst="..image\_name)

Sorry friend, but I can not perform the tests on the console (windows), the library is not supported in the corona simulator.

But the print in the console corresponds to the 3 screen image, where it shows the sourceName and the DestName

Is there any way to debug by the android device?

You can use ADB to debug android devices.  At the command prompt type adb logcat Corona:v *:s to read the console log.

I notice from your third screen shot that the src file name had what looks like 2 | after the jpg?  This would definitely cause a problem.

Not my friend, this is just a character to split the 2 string

native.showAlert( “CHECK FILES NAME”, “src=”…tmp_image_name …" || dst="…image_name )

FYI, \n does the same thing and is cleaner.

Try forcing all filenames into lowercase as aA.jpg is not the same as aa.jpg

Folks,

I was able to resolve after removing the function to check if the file exists in the function “copyFile”

It means that after running the “doesFileExist” function the system loses or changes the path of the temporary file.

Would that be a bug?

You have mixed up your file names.  Try this

local tmp\_image\_name = "copied.jpg" local check = copyFile(image\_name, system.TemporaryDirectory, tmp\_image\_name, system.DocumentsDirectory, true )

It does not work, I’ve tried and it returns me the same error.

Were you able to identify the cause of the error on line 88? I could not.

Yes, the code is trying to read from an invalid file handle.  The error is because the function cannot find the filename you are passing in as srcName and  srcPath.  

Change your code to this and post the output…

if img\_temp then   image\_name = getRandomId(8)..".jpg"  print("src="..tmp\_image\_name, "dst="..image\_name)   local check = copyFile(tmp\_image\_name, system.TemporaryDirectory, image\_name, system.DocumentsDirectory, true )   toast.show( "copy result: " .. check ) end

Hello

The first image shows the temp_file_name that is generated randomly as soon as the user enters the screen.

The second image, after the user select the image, I used the function to get if the image with “temp_file_name” is in the temporary directory of the system, and the function returned me true (this I already knew because I can show the image on the screen From the temporary directory)

The third image shows that both the source (temp) and destination file have a valid name, so I pass to the “copyFile” function a valid file to a valid destination.

Is this a bug?

What I asked is to see what this line printed (in the console) as text not screenshots.

print("src="..tmp\_image\_name, "dst="..image\_name)

Sorry friend, but I can not perform the tests on the console (windows), the library is not supported in the corona simulator.

But the print in the console corresponds to the 3 screen image, where it shows the sourceName and the DestName

Is there any way to debug by the android device?

You can use ADB to debug android devices.  At the command prompt type adb logcat Corona:v *:s to read the console log.

I notice from your third screen shot that the src file name had what looks like 2 | after the jpg?  This would definitely cause a problem.

Not my friend, this is just a character to split the 2 string

native.showAlert( “CHECK FILES NAME”, “src=”…tmp_image_name …" || dst="…image_name )

FYI, \n does the same thing and is cleaner.

Try forcing all filenames into lowercase as aA.jpg is not the same as aa.jpg