Page 1 of 1
save image from url
Posted: Thu Nov 29, 2018 4:15 am
by cusingerBUSCw5N
Hi. I am trying to save a url image to a mobile device.
I have tried SAVING IT:
useline5 is the filename "
https://www.yoururlhere.com/image.png" (edited by Heather as the original example url was a porn site
)
Code: Select all
put useline5 into gheaderimage
load url gheaderimage as timage
export timage to file specialFolderPath("documents") & "/image_header.png" as png
I have tried ACCESSING it:
Code: Select all
put url("binfile:" &specialFolderPath("documents") & "/image_header.png" ) into gheaderimage
ACCESSING it definitely doesn't work. I'm not sure whether it is the code that is the problem, or whether the code for saving is wrong.
---------
I had thought that
Code: Select all
load url gheaderimage with message showHeader
was working. But I now realize that it only works because it had been cached. When I try to change gheaderimage, it doesn't change. So I need to know how to display an image accessed with a url. All my headers are now blank, because I got rid of the caching code.
-------
Any help would be appreciated.
Thanks in advance
Re: save image from url
Posted: Thu Nov 29, 2018 5:06 pm
by bn
I tried this on a Mac/desktop in LC 9.0.2 RC1 and it worked.
You have to adapt the code to your code. (e.g. I download to desktop, I use a script local etc.)
All code is in the button. I unload before load.
Please see also the User Guide for load. (around p 446/447 for the LC 9.0.2 User Guide)
The dictionary entry for load is not very helpful.
Maybe this works also on mobile.
Kind regards
Bernd
PS please don't post anything that Heather has to edit
Re: save image from url
Posted: Thu Nov 29, 2018 5:58 pm
by cusingerBUSCw5N
oohhh...I'm sorry ...I had no idea about the site!
And the solution works. Thanks!
Re: save image from url
Posted: Fri Nov 30, 2018 2:44 am
by cusingerBUSCw5N
Unfortunately....more complications. The solution worked as long as the image was directly declared in
Code: Select all
local gheaderimage = "https://livecode.com/wp-content/uploads/2018/04/multiplatform-2-1018x1024.jpg"
when the image comes in through the results of a data request and is put in a variable (tuse) and then put into global gheaderimage:
Code: Select all
( put "https://livecode.com/wp-content/uploads/2018/04/multiplatform-2-1018x1024.jpg" into tuse
global gheaderimage
put tuse into gheaderimage
it returns with a tstatus of "contacted":
Code: Select all
put the URLStatus of gheaderimage into tStatus
... not cached. When I changed the code to allow a tstatus to be cached or contacted, the image does not appear.
Just guessing...but does "contacted" mean that Livecode didn't have time to grab the whole image? Regardless... I have no idea how to fix it. I have attached the stack (without bad urls)
Re: save image from url
Posted: Fri Nov 30, 2018 5:20 am
by cusingerBUSCw5N
I have tried another example, and when I bring in a different image, it gives me "error" - instead of connected. When I typed in the address, it gives me empty as a status.
This is the code that gives me empty.
Code: Select all
put "https://www.mywebsite/images/paloalto.png" into gheaderimage
put the URLStatus of gheaderimage into tStatus
If I bring in the same information, but put it in a variable, it returns an error:
Code: Select all
put "https://www.mywebsite/images/paloalto.png" into useline5
put useline5 into gheaderimage
put the URLStatus of gheaderimage into tStatus
The app depends on dynamic loading of these images, so I have to use script to load them. But this method obviously isn't working.
Re: save image from url
Posted: Fri Nov 30, 2018 5:33 pm
by AndyP
Try using libUrlDownloadToFile instead
Code: Select all
on mouseUp
set the itemdel to slash
put "https://fileinfo.com/img/icons/files/128/livecode-9094.png" into tUrl
--must have a filename to save too!
put specialFolderPath("desktop") & slash & item -1 of tUrl into tDestPath
libUrlDownloadToFile tUrl, tDestPath, "DownloadComplete"
end mouseUp
on DownloadComplete pUrl, pStatus
If pStatus = "Downloaded" then
answer information "Picture saved!"
else
answer warning "Picture failed to download."
end if
unload url pUrl
end DownloadComplete
Re: save image from url
Posted: Fri Nov 30, 2018 8:27 pm
by cusingerBUSCw5N
So it finally works.
I found that the link I was bringing in has a hidden character.
So this works:
Code: Select all
put useline5 into gheaderimage
set the itemdel to slash
put useline5 into turl0 //begins fixing the hidden character
find "png" in turl0
put offset("png",turl0) + 2 into twhat
put char 1 to twhat of turl0 into turl //hidden character is removed
put specialFolderPath("documents") & slash & "image_header.png" into tDestPath
libUrlDownloadToFile tUrl, tDestPath, "DownloadComplete"
put url("binfile:" &specialFolderPath("documents") & "/image_header.png" ) into gheaderimage
put gheaderimage into image "header" of card "homepage"
end if
Thanks for your help!