I am trying to copy my pre-populated database to a writable directory (I am thinking SD card), this needs to basically copy my whole assets folder within my phonegap android app.
I have done days of research and due to my very limited knowledge of java, I cannot seem to create this java plugin to do the simple copy and then from there I am not entirly certain how to actually call this plugin from my HTML/ Javascript.
Below is the current java plugin I have been working on using sample code found on the net, can someone please help guide me in the right direction.
JAVA PLUGIN:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONArray;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class DataBaseHelper extends Plugin
{
@Override
public PluginResult execute(String arg0, JSONArray arg1, String arg2)
{
try
{
String pName = this.getClass().getPackage().getName();
this.copy("Databases.db","/data/data/"+pName+"/app_database/");
this.copy("0000000000000001.db","/data/data/"+pName+"/app_database/file__0/");
}
catch (IOException e)
{
e.printStackTrace();
}
// TODO Auto-generated method stub
String value = "OK";
return new PluginResult(PluginResult.Status.OK, value);
}
//Copy Paste this function in the class where you used above part
void copy(String file, String folder) throws IOException
{
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
InputStream in = this.ctx.getAssets().open(file);
OutputStream out = new FileOutputStream(folder+file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close(); out.close();
}
}
JAVASCRIPT PLUGIN CALL:
<script type="text/javascript" charset="utf-8" src="taxapp.js"></script>
function onDeviceReady() // CALLING THIS ON BODY LOAD
{
dataCapture();
window.plugins.DataBaseHelper.copy("",function(data)
{
alert("plugin called part 1");
// nothing to do here
},
function()
{
alert("plugin called part 2");
console.warn("Error calling plugin");
});
}
Any help would be much appreciated as I need to get this fixed today. Thanks in advance.