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);
}

エントリーとは?データベース パス フォームのアセット フォルダーはどこに書く必要がありますか? 最後の行で成功と言いましたが、それを定義していませんでしたか? 代わりに win と書く必要がありますか?

4

2 に答える 2

1

ファイル API は、アセット ディレクトリ内のファイルにアクセスできません。プラグインを作成する必要があります。

于 2011-12-15T18:34:45.093 に答える
1

メインアクティビティ(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

于 2011-12-16T14:25:01.300 に答える