can't load image from documents directory

I've done a lot of testing and experimenting on this, but I still can't see what's wrong.

I'm downloading a PNG image off a website and trying to display it. However, my app crashes at this line:

1
2
3
4
5
local image = display.newImage(
                        fileNameString,
                        system.DocumentsDirectory,
                        display.contentCenterX - 10,
                        display.contentCenterY)

Hard to say without seeing the rest of the code.

How are you downloading the image of the site? Did you save the path for the downloaded file? You will need that info to show the image.

Here's a good link that shows all that:
http://developer.anscamobile.com/content/simple-image-download

I've pulled out the main code involved with minor mods.Thanks for looking.

1
2
3
4
5
6
7
8
9
10
11
12
13
local fileNameString = "blah.png"
local imagePathString = "/dir1/dir2/blah.png"
local imagePath = system.pathForFile( fileNameString, system.DocumentsDirectory )
local urlString = "http://www.url.com"..imagePathString
imageFile, reason = io.open( imagePath, "w+b" )
http.request{ url = urlString, sink = ltn12.sink.file(imageFile) }
-- note the file downloads fine
local image = display.newImage(
        fileNameString,
        system.DocumentsDirectory,
        display.contentCenterX - 10,
        display.contentCenterY
)

Are you sure the file is downloading? How do you know? If there is a download, what is the size of the file? Can you open it?

This is really driving me crazy. I've just done another test, which is to copy the file as downloaded into the simulator's sandbox documents folder and try the following code to simply load the image in the simulator:

1
2
3
4
5
6
7
local imagePath = system.pathForFile( "blah.png", system.DocumentsDirectory )
local image = display.newImage(
        imagePath,
        system.DocumentsDirectory,
        display.contentCenterX - 10,
        display.contentCenterY
)

In reply to your previous post, I've grabbed it from the /data/ folder on my phone using adb and viewed it. It also displays *on my phone* if it's loaded from the resource folder. I just can't get it to display from the documents directory.

Here, try this. Create a main.lua file, copy and paste this into the file and try running it in the simulator. It should display the Google logo to the bottom and right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local http = require("socket.http")
local ltn12 = require("ltn12")
 
--this will download an image from the web
function downloadImage(filename,suburlpath,tld,x,y)
        local fileNameString = filename
        local imagePathString = suburlpath
        local imagePath = system.pathForFile( fileNameString, system.DocumentsDirectory )
        local myfile = io.open(imagePath, "w+b")
        http.request{ url = tld .. imagePathString, sink = ltn12.sink.file(myfile) }
        local image = display.newImage(fileNameString,system.DocumentsDirectory, x, y);
end
 
--call the function
downloadImage("blah",
                        "/images/logos/ps_logo2.png",
                        "http://www.google.com",
                        display.contentCenterX - 10, 
                        display.contentCenterY
                        )

I got quite a long delay before the "Corona trial" pop-up appeared, then nothing, just a blank screen. It worked in the simulator however. Appreciate your time. Why wouldn't this be working? I'm running a Dell Streak with Android 2.2.

Edit: I think it was killed for taking too long to start??

1
2
3
W/ActivityManager(  149): Launch timeout has expired, giving up wake lock!
W/ActivityManager(  149): Activity idle timeout for HistoryRecord{470ed140 com.blah.blah.apps/.MyCoronaActivity}
I/ActivityManager(  149): Displayed activity com.blah.blah.apps/.MyCoronaActivity: 25235 ms (total 25235 ms)

No probs.

When I get my Android device in a couple of weeks I'll be more effective. At this time, it's all simulator.

My main work in is iPhone and iPad.

Anyone else want to take a crack at this :)

Thanks again. I'm going to sleep on it. I'm sure it will be something simple, but there are quite a few gotchas to Corona development on Windows as I've found. Or maybe it's just me :)

Hi i have the same problem witch i described in this tread:
------
https://developer.anscamobile.com/content/network

Here is the problem:
------
I've tried the above code, it builds and works fine in the Corona emulator but when i install it in my HTC Desire HD - Android 2.2 it just halts when it is going to show the image. The program gets unresponsive.

If I exit the program and go in to the program manager i can see that the data for my program is now 8kb instead of 0kb, exactly as big as the image I'm trying to load is.

What might be the problem?

EDIT: I downloaded an build the example: http://developer.anscamobile.com/content/simple-image-download and it din not work either.. Same thing here, the app data grow from 0 to 4kb (witch is the image) when i pres download but no image shows up. At least this one does not hang up.

Could this be related to: http://developer.anscamobile.com/forum/2011/02/09/images-directory-dont-work-android-using-windows-sdk#comment-21778 ?

Have you tried saving the image to "/" instead of sub-folders?

Just a thought. I just had some issues with my apps not loading due to images in sub-folders..

Did you set permission to access the internet? Without it your android device won't be able to download stuff from the net.

It's a known issue: can't load images from sub directories. Close the thread :-)

Well, hope it gets fixed soon.
I actually encountered this problem on ipod and iphone, for both the resource and document directory. If I just do something like display.newImage("folder/image.png"... it works ok. If I use the pathForFile it doesn't. The real problem is the documents directory, where the pathForFile is needed, and I actually really need this to work.
Anyone know a work around for the documents directory? :\

views:1998 update:2011/10/9 9:57:41
corona forums © 2003-2011