如何在phonegap中使用copyto api

发布于 2024-12-21 21:44:47 字数 516 浏览 3 评论 0原文

我想将 sqlite db 复制到数据库文件夹,但不知道如何使用 copyto api:

function win(entry) {
console.log("New Path: " + entry.fullPath);
}

function fail(error) {
 alert(error.code);
}

function copyFile(entry) {
 var parent = document.getElementById('parent').value,
    parentEntry = new DirectoryEntry({fullPath: parent});

 // copy the file to a new directory and rename it
 entry.copyTo(parentEntry, "file.copy", success, fail);
}

entry 是什么?我必须在哪里编写资产文件夹中的数据库路径?是否错误地在最后一行中表示成功但没有定义它?我必须写 win 吗?

i want to copy a sqlite db to the database folder but dont know how to use copyto api:

function win(entry) {
console.log("New Path: " + entry.fullPath);
}

function fail(error) {
 alert(error.code);
}

function copyFile(entry) {
 var parent = document.getElementById('parent').value,
    parentEntry = new DirectoryEntry({fullPath: parent});

 // copy the file to a new directory and rename it
 entry.copyTo(parentEntry, "file.copy", success, fail);
}

what is entry? where to i have to write my db path form assets folder? is it mistyping that in the last line it said success but didn't define it? do i have to write win instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷雨优美回忆 2024-12-28 21:44:47

文件 API 无法访问资产目录内的文件。您需要编写一个插件。

The File API can't access files inside the asset directory. You'll need to write a plugin.

昔日梦未散 2024-12-28 21:44:47

我通过向我的主要活动(java 代码)添加一些代码来做到这一点:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

     import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;

    public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String pName = this.getClass().getPackage().getName();
        try
        {
        this.copy("Databases.db","/data/data/"+pName+"/app_database/");
        }

        catch (IOException e)


    {
        e.printStackTrace();
    }

    }

    void copy(String file, String folder) throws IOException 
    {

        File CheckDirectory;
        CheckDirectory = new File(folder);
        if (!CheckDirectory.exists())
        { 
            CheckDirectory.mkdir();
        }

        InputStream in = getApplicationContext().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();

    }
    }

来源:
http://gauravstomar.blogspot.com/2011/08/prepopulate -sqlite-in-phonegap.html

I did it by adding some code to my main activity(java code):

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

     import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;

    public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String pName = this.getClass().getPackage().getName();
        try
        {
        this.copy("Databases.db","/data/data/"+pName+"/app_database/");
        }

        catch (IOException e)


    {
        e.printStackTrace();
    }

    }

    void copy(String file, String folder) throws IOException 
    {

        File CheckDirectory;
        CheckDirectory = new File(folder);
        if (!CheckDirectory.exists())
        { 
            CheckDirectory.mkdir();
        }

        InputStream in = getApplicationContext().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();

    }
    }

source:
http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文