User Tools

Site Tools


script_load_data_from_internet

====== About the script ====== * Purpose : This script is a sample code to loads data from the Internet. It will load an image and display it, but it could be derived to load pretty much anything else, JSON, XML, audio, etc. * Author : Pierre Hébert. ====== How to use the script ====== First, make sure the Internet permission is added to the launcher through the installation of the plugin found at [[http://www.pierrox.net/android/applications/lightning_launcher/permissions/|http://www.pierrox.net/android/applications/lightning_launcher/permissions/]]. Import the script, then set it as the "Create" script on a Custom View item. It will create an image with a random content. ====== Script code ====== <sxh javascript> 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; </sxh>

script_load_data_from_internet.txt · Last modified: 2015/04/02 16:49 by pierrox