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:

[lua]local image = display.newImage(
fileNameString,
system.DocumentsDirectory,
display.contentCenterX - 10,
display.contentCenterY)[/lua]

I have a print statement just before and just after this line, and it’s not printing the one after, so I know it’s not going past here.

To test the PNG is ok, I put it in the main app folder and built it into the app, and displayed it when the app starts, which succeeds. It’s also fine on the simulator. Any thoughts welcome.
[import]uid: 23949 topic_id: 6289 reply_id: 306289[/import]

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

[import]uid: 8045 topic_id: 6289 reply_id: 21662[/import]

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

[lua]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
) [import]uid: 23949 topic_id: 6289 reply_id: 21668[/import]

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?
[import]uid: 8045 topic_id: 6289 reply_id: 21674[/import]

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. [import]uid: 23949 topic_id: 6289 reply_id: 21676[/import]

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:

[lua]local imagePath = system.pathForFile( “blah.png”, system.DocumentsDirectory )
local image = display.newImage(
imagePath,
system.DocumentsDirectory,
display.contentCenterX - 10,
display.contentCenterY
)[/lua]

Simple enough, but I get the following in the debug log:

[lua]Copyright © 2009-2010 A n s c a , I n c .
Version: 2.0.0
Build: 2011.288
WARNING: display.setStatusBarMode() not supported in the simulator for the current device
WARNING: Failed to find image(C:\Users\blah\Documents\Corona Simulator\Sandbox\program-747C872A03E8FAF7C34B739A5E7ECF49
\Documents\blah.png)
Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function <?:518>
(tail call): ?
C:\app\main.lua:16: in function ‘main’
C:\app\main.lua:39: in mainRuntime error: assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function <?:518>
(tail call): ?
C:\app\main.lua:16: in function ‘main’
C:\app\main.lua:39: in main chunk[/lua]

I’ve verified that file and path exist (guess you’ll have to take my word for it). How could this fail? It’s really busting my &*^*& [import]uid: 23949 topic_id: 6289 reply_id: 21675[/import]

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.

[lua]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
)[/lua] [import]uid: 8045 topic_id: 6289 reply_id: 21677[/import]

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??

[lua]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)[/lua]
[import]uid: 23949 topic_id: 6289 reply_id: 21679[/import]

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 :slight_smile: [import]uid: 8045 topic_id: 6289 reply_id: 21682[/import]

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 :slight_smile: [import]uid: 23949 topic_id: 6289 reply_id: 21683[/import]

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.
[import]uid: 28895 topic_id: 6289 reply_id: 21687[/import]

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… [import]uid: 20046 topic_id: 6289 reply_id: 22153[/import]

Did you set permission to access the internet? Without it your android device won’t be able to download stuff from the net. [import]uid: 5712 topic_id: 6289 reply_id: 22158[/import]

It’s a known issue: can’t load images from sub directories. Close the thread :slight_smile: [import]uid: 23949 topic_id: 6289 reply_id: 22164[/import]

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? :\ [import]uid: 8486 topic_id: 6289 reply_id: 22484[/import]

hi the above code works fine for my problem thanks [import]uid: 115284 topic_id: 6289 reply_id: 104429[/import]