Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:17:37+00:00 2026-05-31T17:17:37+00:00

I am trying to copy my pre-populated database to a writable directory (I am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:17:38+00:00Added an answer on May 31, 2026 at 5:17 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm Unable to copy SQLite database from assests folder to device memory(trying on emulator).
I'm trying copy a single file from the Plugin directory inside of my Wordpress
I am trying to copy the contents of certain folders to another folder using
I'm trying to copy a directory using the Ant copy task. I am a
I am trying to copy the string $str = ' this is my string';
Im trying to copy a directory to a new location. So I am using
I'm trying to copy information from an iframe to my document, I wrote this
Trying to copy a database into a new database, using a PHP script that
I am just trying to copy a database from one server to another one...
I'm trying to copy a folder from my computer to my android phone using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.