【发布时间】:2012-04-05 21:53:18
【问题描述】:
我正在尝试将我预先填充的数据库复制到一个可写目录(我想是 SD 卡),这基本上需要将我的整个 assets 文件夹复制到我的 phonegap android 应用程序中。
我已经做了几天的研究,由于我对 java 的了解非常有限,我似乎无法创建这个 java 插件来做简单的复制,然后我不确定如何从我的 HTML 中实际调用这个插件/ Javascript。
以下是我一直在使用网上找到的示例代码开发的当前 java 插件,有人可以帮助指导我正确的方向。
JAVA 插件:
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 插件调用:
<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");
});
}
任何帮助将不胜感激,因为我今天需要解决这个问题。提前致谢。
【问题讨论】:
标签: java android sqlite plugins cordova