use of an image on android device

hi all,

how do I use an image on an android build for device? 

I’ve build an app for IOS which works fine and in the simulator everything is fine. On the android device however the images won’t load. 

I use a display.imageRect statement directly accessing the .png file in the resourcedirectory. This does not work. 

So I tried copying the file from the resource to the documentsdirectory, still the image does not display. I have tried the .png.txt copy “hack”. Did not work. 

What is the most easy and common way to display an image within android? 

thanks in advance. 

I use the latest corona build and a Galaxy Tab 7 for testing. 

Android is very case sensitive about filenames, the simulator is not. Try running the app on the tablet in debugging mode and errors will show up in the log. I found it very helpful:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

:slight_smile: I’ve been debugging for days, so that should not be the problem. 

It looks like my code does not respond to the resourcedirectory files. None of them, not just one, but all combinations and naming I gave them.

Hello @Elbert,

Can you post some of your code here? “display.imageRect” is not a valid API call. Do you mean “display.newImageRect()”?

Best regards,

Brent

I have tried the following:

[lua]

function copyFile( srcName, srcPath, dstName, dstPath )

    --copy the source file to the destination file

    

    local rfilePath = system.pathForFile( srcName, srcPath )

    local wfilePath = system.pathForFile( dstName, dstPath )

    print (rfilePath )

    print (wfilePath )

    

    

    local rfh = io.open( rfilePath, “rb” )

    local wfh = io.open( wfilePath, “wb” )

   print (rfh)

    if not ( wfh ) then

        print( “writeFileName open error!” )

        --return false

    else

        --read the file from ‘system.ResourceDirectory’ and write to the destination directory

        local data = rfh:read( “*a” )

        if not ( data ) then

            print( “read error!” )

          –  return false

        else

            if not ( wfh:write( data ) ) then

                print( “write error!” )

            –    return false

            end

        end

    end

    

    --clean up file handles

    rfh:close()

    wfh:close()

end

copyFile( “backgrass.txt”, nil, “backgrass.png”, system.DocumentsDirectory)

local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )

[/lua]

the ADB log states:

 

nil --this is the first print I use

/data/data/some app/app_data/backgrass.png – second print statement

Runtime error

bad argument #1 to ‘open’ (string expected, got nil) – this is the same nil as in my first print

stack traceback:

[C] : in function ‘open’

?: in function ‘copyFile’

?: in main chunk

 

 

 

as I can see it looks like the pathforfile module isn’t working as it should. 

 

and the file backgrass.txt is obviously in my resourcefiles…all lowercase.

If I were you, I would make sure the passed in parameters are not nil.  That way you are sure of what’s going on:

function copyFile( srcName, srcPath, dstName, dstPath )       --copy the source file to the destination file     local sourcePath = srcPath or system.ResourceDirectory     local destPath = dstPath or system.DocumentsDirectory     if srcName == nil then          error("No source name passed");     end     if dstName == nil then          error("No destination name passed");     end         local rfilePath = system.pathForFile( srcName, srcPath )     local wfilePath = system.pathForFile( dstName, dstPath )

or something like that.

Rob

I tried this:

[lua]

function copyFile()

    

    system.pathForFile( “backgrass.txt”, system.ResourceDirectory )

    local rfilePath = system.pathForFile( “backgrass.txt”, system.ResourceDirectory )

    local wfilePath = system.pathForFile( “backgrass.png”, system.DocumentsDirectory )

    print (rfilePath )

    print (wfilePath )

    

    

    local rfh = io.open( rfilePath, “rb” )

    local wfh = io.open( wfilePath, “wb” )

  – print (rfh)

    if not ( wfh ) then

        print( “writeFileName open error!” )

    else

        --read the file from ‘system.ResourceDirectory’ and write to the destination directory

        local data = rfh:read( “*a” )

        if not ( data ) then

            print( “read error!” )

        else

            if not ( wfh:write( data ) ) then

                print( “write error!” )

            end

        end

    end

    

    --clean up file handles

    rfh:close()

    wfh:close()

end

copyFile()-- “backgrass.txt”, “backgrass.png”)

system.pathForFile( “backgrass.png”, system.DocumentsDirectory )

local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )

[/lua]

And the function did not return any errors, the print statements gave me a nice path to the files. 

Except the image still was not showing on my android device…

I feel like I’m jumping through hoops, just to display an image. Isn’t there an easier way to do this?

Thank you all for thinking along…I’ve found the problem and the solution. As often the problem was between the chair and the keyboard…as in me. 

I was building for android, and debugging on my samsung device. When I builded for samsung everything works like a charm…

strange as it is.

EDIT: the problem is solved when building voor Samsung, Whenever I build for android the problem is still there. The app is doing fine, except for the pictures. Ads are showing, text is showing. 

It seems like copy of the image file isn’t working when I build for google play and it is working when I build for Samsung with exactly the same code…strange isn’t it?

Android is very case sensitive about filenames, the simulator is not. Try running the app on the tablet in debugging mode and errors will show up in the log. I found it very helpful:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

:slight_smile: I’ve been debugging for days, so that should not be the problem. 

It looks like my code does not respond to the resourcedirectory files. None of them, not just one, but all combinations and naming I gave them.

Hello @Elbert,

Can you post some of your code here? “display.imageRect” is not a valid API call. Do you mean “display.newImageRect()”?

Best regards,

Brent

I have tried the following:

[lua]

function copyFile( srcName, srcPath, dstName, dstPath )

    --copy the source file to the destination file

    

    local rfilePath = system.pathForFile( srcName, srcPath )

    local wfilePath = system.pathForFile( dstName, dstPath )

    print (rfilePath )

    print (wfilePath )

    

    

    local rfh = io.open( rfilePath, “rb” )

    local wfh = io.open( wfilePath, “wb” )

   print (rfh)

    if not ( wfh ) then

        print( “writeFileName open error!” )

        --return false

    else

        --read the file from ‘system.ResourceDirectory’ and write to the destination directory

        local data = rfh:read( “*a” )

        if not ( data ) then

            print( “read error!” )

          –  return false

        else

            if not ( wfh:write( data ) ) then

                print( “write error!” )

            –    return false

            end

        end

    end

    

    --clean up file handles

    rfh:close()

    wfh:close()

end

copyFile( “backgrass.txt”, nil, “backgrass.png”, system.DocumentsDirectory)

local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )

[/lua]

the ADB log states:

 

nil --this is the first print I use

/data/data/some app/app_data/backgrass.png – second print statement

Runtime error

bad argument #1 to ‘open’ (string expected, got nil) – this is the same nil as in my first print

stack traceback:

[C] : in function ‘open’

?: in function ‘copyFile’

?: in main chunk

 

 

 

as I can see it looks like the pathforfile module isn’t working as it should. 

 

and the file backgrass.txt is obviously in my resourcefiles…all lowercase.

If I were you, I would make sure the passed in parameters are not nil.  That way you are sure of what’s going on:

function copyFile( srcName, srcPath, dstName, dstPath )       --copy the source file to the destination file     local sourcePath = srcPath or system.ResourceDirectory     local destPath = dstPath or system.DocumentsDirectory     if srcName == nil then          error("No source name passed");     end     if dstName == nil then          error("No destination name passed");     end         local rfilePath = system.pathForFile( srcName, srcPath )     local wfilePath = system.pathForFile( dstName, dstPath )

or something like that.

Rob

I tried this:

[lua]

function copyFile()

    

    system.pathForFile( “backgrass.txt”, system.ResourceDirectory )

    local rfilePath = system.pathForFile( “backgrass.txt”, system.ResourceDirectory )

    local wfilePath = system.pathForFile( “backgrass.png”, system.DocumentsDirectory )

    print (rfilePath )

    print (wfilePath )

    

    

    local rfh = io.open( rfilePath, “rb” )

    local wfh = io.open( wfilePath, “wb” )

  – print (rfh)

    if not ( wfh ) then

        print( “writeFileName open error!” )

    else

        --read the file from ‘system.ResourceDirectory’ and write to the destination directory

        local data = rfh:read( “*a” )

        if not ( data ) then

            print( “read error!” )

        else

            if not ( wfh:write( data ) ) then

                print( “write error!” )

            end

        end

    end

    

    --clean up file handles

    rfh:close()

    wfh:close()

end

copyFile()-- “backgrass.txt”, “backgrass.png”)

system.pathForFile( “backgrass.png”, system.DocumentsDirectory )

local catImage = display.newImage( “backgrass.png”, system.DocumentsDirectory, 0, 100 )

[/lua]

And the function did not return any errors, the print statements gave me a nice path to the files. 

Except the image still was not showing on my android device…

I feel like I’m jumping through hoops, just to display an image. Isn’t there an easier way to do this?

Thank you all for thinking along…I’ve found the problem and the solution. As often the problem was between the chair and the keyboard…as in me. 

I was building for android, and debugging on my samsung device. When I builded for samsung everything works like a charm…

strange as it is.

EDIT: the problem is solved when building voor Samsung, Whenever I build for android the problem is still there. The app is doing fine, except for the pictures. Ads are showing, text is showing. 

It seems like copy of the image file isn’t working when I build for google play and it is working when I build for Samsung with exactly the same code…strange isn’t it?