我想我的pre填充的数据库复制到一个可写目录(我想SD卡),这需要从根本上复制在我的PhoneGap的Android应用我的整个资产的文件夹。
我已经做了研究和天由于我的非常有限的Java知识,我似乎无法创建这个Java插件做了简单的复制,然后从那里我不entirly某些如何实际调用这个插件从我的HTML / Javascript的。
下面是当前的Java插件,我一直在使用网上找到的样本code,可有人请帮助指导我在正确的方向。
Java插件:
进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口org.json.JSONArray;
进口com.phonegap.api.Plugin;
进口com.phonegap.api.PluginResult;
公共类DataBaseHelper扩展插件
{
@覆盖
公共PluginResult执行(字符串为arg0,JSONArray ARG1,串ARG2)
{
尝试
{
字符串PNAME = this.getClass()getPackage()的getName()。
this.copy(Databases.db,/数据/数据/+ PNAME +/ app_database /);
this.copy(0000000000000001.db,/数据/数据/+ PNAME +/ app_database / file__0 /);
}
赶上(IOException异常E)
{
e.printStackTrace();
}
// TODO自动生成方法存根
字符串值=确定;
返回新PluginResult(PluginResult.Status.OK,价值);
}
//复制粘贴此功能的类,你上面的一部分使用
无效副本(字符串文件,字符串文件夹)抛出IOException异常
{
文件CheckDirectory;
CheckDirectory =新的文件(文件夹);
如果(!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
InputStream的时间= this.ctx.getAssets()打开(文件)。
的OutputStream OUT =新的FileOutputStream(文件夹+文件);
//传输的字节从以出
byte []的BUF =新的字节[1024];
INT LEN;而((LEN = in.read(BUF))大于0)out.write(buf中,0,的len);
附寄(); out.close();
}
}
JAVASCRIPT PLUGIN CALL:
<脚本类型=文/ JavaScript的字符集=utf-8SRC =taxapp.js>< / SCRIPT>
功能onDeviceReady()//调用这个ON承载车身
{
dataCapture();
window.plugins.DataBaseHelper.copy(,功能(数据)
{
警报(插件称为第1部);
// 在这儿无事可做
},
功能()
{
警报(插件调用的部分2);
console.warn(错误调用插件);
});
}
任何帮助将是非常美联社preciated因为我需要这个固定的今天拿到。先谢谢了。
解决方案尝试使用 this.ctx
而不是这
和 getApplicationContext()
的插件
不是一个上下文本身,但它拥有的真正的上下文在其 CTX
字段。
添加一个.js文件与下面的内容:
VAR DataBaseHelper =功能(){};
DataBaseHelper.prototype.copy =功能(参数,可以成功,失败)
{
返回PhoneGap.exec(函数(参数)
{
成功(参数);
},
功能(参数)
{
失败(参数);
},DataBaseHelper,复制,[PARAMS]);
};
PhoneGap.addConstructor(函数()
{
PhoneGap.addPlugin('DataBaseHelper',新DataBaseHelper());
PluginManager.addService(DataBaseHelper,full.package.name.DataBaseHelper);
});
然后调用插件的PhoneGap初始化之后的任何时间:
window.plugins.DataBaseHelper.copy(,功能(数据)
{
// 在这儿无事可做
},
功能()
{
console.warn(错误调用插件);
});
此外,PhoneGap的-1.0.0之后,你在你的RES / xml目录,在这里你需要增加需要一个plugins.xml文件
<插件名称=DataBaseHelper值=full.package.name.DataBaseHelper/>
要知道,如果PhoneGap的已初始化的最好方法是通过在javascript调用这个方法:
document.addEventListener(deviceready,onDeviceReady,假);
功能onDeviceReady()
{
//这里调用你的插件
}
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.
解决方案Try using this.ctx
instead of this
and getApplicationContext()
, the Plugin
isn't a Context in itself, but it holds the 'real' context in its ctx
field.
Add a .js file with the content below:
var DataBaseHelper = function() {};
DataBaseHelper.prototype.copy = function(params, success, fail)
{
return PhoneGap.exec(function(args)
{
success(args);
},
function(args)
{
fail(args);
}, 'DataBaseHelper', 'copy', [params]);
};
PhoneGap.addConstructor(function()
{
PhoneGap.addPlugin('DataBaseHelper', new DataBaseHelper());
PluginManager.addService("DataBaseHelper","full.package.name.DataBaseHelper");
});
Then call the plugin anytime after PhoneGap is initialized:
window.plugins.DataBaseHelper.copy("",function(data)
{
// nothing to do here
},
function()
{
console.warn("Error calling plugin");
});
Also, after phonegap-1.0.0, you need a plugins.xml file in your res/xml directory, where you need to add:
<plugin name="DataBaseHelper" value="full.package.name.DataBaseHelper"/>
The best way to know if phonegap has initialized is by calling this method in javascript:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
//call your plugin here
}