I’m having trouble copying a file inside a script.
I’m having trouble copying a file inside a script. I’m deleting the current LL desktop wallpaper, and then trying to copy a new one in there for a wallpaper changer. I have Java code to copy the file, but I’m having trouble with the javascript translation. Perhaps there’s an easy way to copy files inside LL scripts?
]]>
« I purchased LL. How do I get my desktop from the trial version into the purchased version? (Previous Post)
(Next Post) Question to Pierre Hébert: »
< ![CDATA[
There isn’t. LL isn’t meant to be a file manager 😉
You’ll need two Streams connected for this.
]]>
< ![CDATA[
Yeah, I figured. I’m having to use a hack Pierre recommended since the LL wallpaper isn’t accessible via the API yet. I guess I’ll just have to find a way to handle the Java primitives in javascript.
]]>
< ![CDATA[
I confirm this, you need to use streams but this is not exactly obvious.
The loop is quite easy, but you need to use a special syntax to allocate a byte buffer, something like this:
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
Also don’t forget to close streams in a finally statement.
]]>
< ![CDATA[
That’s the syntax I was looking for! Although, I got it to work doing this:
LL.bindClass(“java.io.File”);
LL.bindClass(“java.io.FileOutputStream”);
LL.bindClass(“java.io.FileInputStream”);
var test = new File(“/storage/external_SD/Android/data/net.pierrox.lightning_launcher/files/test-1.txt”);
var test2 = new File(“/storage/external_SD/Android/data/net.pierrox.lightning_launcher/files/blah.txt”);
copy(test, test2);
function copy(src, dst){
var input = new FileInputStream(src);
var out = new FileOutputStream(dst);
var b = input.read();
while(b != -1)
{
out.write(b);
b = input.read();
}
input.close();
out.close();
}
]]>
< ![CDATA[
That works too!!! (although probably not very efficient, this may cause ANR issues).
You may also need to do the work in another thread, I believe some Android versions will complain about IO on the UI thread.
]]>
< ![CDATA[
Yeah. I just wanted to get a POC working, and I was limited without being able to create a byte array. I think I’m also going to look at a FileChannel operation. It seems to abstract away some of the parts of the file transfer. Thanks again! (Making Java into javascript is hard work!)
]]>
< ![CDATA[
Just a though: maybe java.lang.Runtime.exec with a command line like “cp /from/path /to/path” could do the trick.
(but issues with characters to escape and so on)
]]>
< ![CDATA[
That’s genius! I always forget you can do that. This works like a charm:
LL.bindClass(“java.lang.Runtime”);
var path1 = “/storage/external_SD/Android/data/net.pierrox.lightning_launcher/files/test-1.txt”
var path2 = “/storage/external_SD/Android/data/net.pierrox.lightning_launcher/files/blah.txt”
var rt = Runtime.getRuntime()
rt.exec(“cp ” + path1 + ” ” + path2)
]]>
< ![CDATA[
🙂
]]>