I’d like to use a script to update the box background of an item by referencing a file on my sdcard, but can’t get…
I’d like to use a script to update the box background of an item by referencing a file on my sdcard, but can’t get the setBoxBackground command to work. Could use some help from the script experts here …
]]>
« Title (Previous Post)
< ![CDATA[
Post what you already have, otherwise we can’t tell you whats wrong.
]]>
< ![CDATA[
Here’s what I have … I get a “Can’t find method net.pierrox.lightning_launcher.b.a.k.setBoxBackground(string)” error on line 2. Thanks!
var item=LL.getCurrentDesktop().getItemByName(‘map’);
item.setBoxBackground(‘/storage/emulated/0/Tasker/map.png’);
]]>
< ![CDATA[
setBoxBackground requires an image.
Create an image from the file first, then pass it to setBocBackground. Additionally setBoxBackground requires you to pass a second argument: the state. Normally you want to set this to “nsf”.
see http://www.pierrox.net/android/applications/lightning_launcher/script/reference/net/pierrox/lightning_launcher/script/api/Item.html#setBoxBackground%28net.pierrox.lightning_launcher.script.api.Image,%20java.lang.String%29
]]>
< ![CDATA[
Lukas Morawietz thank you, that did the trick! I have two follow-up questions if you don’t mind:
1. If I use the permissions manager you created to allow internet access, is it possible for the image to be directly linked to a URL (specifically, I want to do a http get of static google maps image)? I’m currently using Tasker to run the http get but because scripting needs LL in the foreground it’s cumbersome
2. I’d like the same script (refresh map image) to run when I scroll the desktop right (so the map page comes into view). What’s the best way of doing this? Stop points? I tried the “resume” event but it doesn’t trigger when the page comes into view, only when the desktop status is changed
]]>
< ![CDATA[
With the internet permission you can use HTTP APIs to fetch data from the internet although this is not exactly straightforward. A simple thing that may work (not tested) is to use a custom view with a single ImageView and use ImageView.setImageUri(“http://your_image“);
]]>
< ![CDATA[
Pierre Hébert would it look something like this? noob at scripts and haven’t used custom view before so here’s what I gathered from looking up online …
var item=LL.getCurrentDesktop().getItemByName(‘map’);
var image= new ImageView();
image.setImageUri(“http://your_image“);
item.setBoxBackground(image, “nsf”);
]]>
< ![CDATA[
No, because setBoxBackground does not need an ImageView but an Image object.
What you need to do instead is add a Custom View item and edit its create script with the following:
LL.bindClass(“android.widget.ImageView”);
var image_view= new ImageView();
image_view.setImageUri(“http://your_image“);
return image_view;
The ImageView is an Android component that may take care of loading the image using the “content resolver”, but again I didn’t tested whether it works with http URIs. To test whether the image can be loaded from a file you can use this URI: “file:///path/to/the/image”.
]]>
< ![CDATA[
Here’s what I tried, but I get a TypeError: Cannot find function setImageUri in object android.widget.ImageView … any advice?
LL.bindClass(“android.widget.ImageView”);
var imgView=new ImageView(LL.getContext());
imgView.setImageUri(‘file:///storage/emulated/0/Tasker/map.png’);
return imgView;
]]>
< ![CDATA[
Oh sorry, my bad, I forgot that the string need to be converted to a Uri object, this way:
imgView.setImageUri(Uri.parse(‘file:///storage/emulated/0/Tasker/map.png’));
]]>
< ![CDATA[
Thanks for the quick response! Tried that too but get the same error …
]]>
< ![CDATA[
Arggg, this is setImageURI not setImageUri, lol
]]>
< ![CDATA[
Thanks! Changing to URI worked, but unfortunately setImageURI does not allow for http retrieval. Looks like one needs to download the file to SD or an image cache first …
]]>
< ![CDATA[
Ok, as expected this was less than obvious, but here is a sample script to fetch an image from a random Internet source:
LL.bindClass(“android.os.Handler”);
LL.bindClass(“android.widget.ImageView”);
LL.bindClass(“java.net.URL”);
LL.bindClass(“java.lang.Thread”);
LL.bindClass(“java.lang.Runnable”);
LL.bindClass(“android.graphics.BitmapFactory”);
var i = new ImageView(LL.getContext());
// cannot use AsyncTask, so use a Thread and a Handler
var handler = new Handler();
new Thread(new Runnable({
run: function(params) {
var con = null;
var is = null;
try {
var url = new URL(“http://lorempixel.com/400/200/“);
con = url.openConnection();
is = con.getInputStream();
var bitmap = BitmapFactory.decodeStream(is);
handler.post(new Runnable({
run: function() {
if(bitmap != null) {
i.setImageBitmap(bitmap);
} else {
// error handling here
}
}
}));
} catch(e) {
return null;
} finally {
if(is != null) try { is.close(); } catch(e) {}
if(con != null) con.disconnect();
}
}
})).start();
return i;
(I’ll put this on the wiki so that it can be imported easily)
]]>
< ![CDATA[
http://www.pierrox.net/android/applications/lightning_launcher/wiki/doku.php?id=script_load_data_from_internet
]]>
< ![CDATA[
Thank you … will try this out
]]>