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

  • SEARCH
  • Home
  • 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 8789807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:38+00:00 2026-06-13T22:30:38+00:00

Considering that my application is still in development, from time to time error appears

  • 0

Considering that my application is still in development, from time to time error appears that have influence on my database connected with the application. Because I have already test data, that I don’t want to enter every time something goes wrong, I want to implement a simple import / export functionality.

I recently stumbled over this question, which is more or less what I want. I show you my code implemented. The export function works fine, I can test it with a SQLIte Database application from the android market. The problem is the import. It shows me the Toast, that everything gone good, but nothing happens. Still, I’m not quite sure if I’m doing it right.

In the MainMenuActivity I’ve implemented following methods:

(...)

public boolean onCreateOptionsMenu(Menu menu){
    menu.add(1, Menu.FIRST, Menu.FIRST, "backup DB").setIcon(R.drawable.ic_action_save);
    menu.add(1, Menu.FIRST+1, Menu.FIRST+1, "import DB").setIcon(R.drawable.ic_action_upload);
    return super.onCreateOptionsMenu(menu);
}

(...)

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case 1:
        backupDatabase();
        return true;
    case 2:
        importDatabase();
        return true;
        default:
        break;
    }
    return super.onOptionsItemSelected(item);
}


private void backupDatabase(){
    try{
         File sd = Environment.getExternalStorageDirectory();
         File data = Environment.getDataDirectory();

         if (sd.canWrite()) {
             String currentDBPath = "//data//challenge.main//databases//DBCHALLENGES";
             String backupDBPath = "DBCHALLENGES";
             File currentDB = new File(data, currentDBPath);
             File backupDB = new File(sd, backupDBPath);

             FileChannel src = new FileInputStream(currentDB).getChannel();
             FileChannel dst = new FileOutputStream(backupDB).getChannel();
             dst.transferFrom(src, 0, src.size());
             src.close();
             dst.close();
             Toast.makeText(getBaseContext(), "Successfully backed up database!", Toast.LENGTH_LONG).show();
         }
    }catch (IOException e){
        Toast.makeText(getBaseContext(), "Error in backing up database!", Toast.LENGTH_LONG).show();
    }
}


private void importDatabase(){
    datasource = new CustomDataSource(this);
    datasource.open();
    try{
        datasource.importDatabase("DBCHALLENGES");
        Toast.makeText(getBaseContext(), "Successfully imported database!", Toast.LENGTH_LONG).show();
    }catch (IOException e){
        Toast.makeText(getBaseContext(), "Error in importing database!", Toast.LENGTH_LONG).show();
    }finally{
        datasource.close();
    }
}

in my CustomDataSource:

public boolean importDatabase(String dbPath) throws IOException{
    return dbHelper.importDatabase(dbPath);
}

and finally in the DBHelper extending SQLiteOpenHelper

public boolean importDatabase(String dbPath) throws IOException{
    close();
    File newDB = new File(dbPath);
    File oldDB = new File("//data//challenge.main//databases//DBCHALLENGES");
    if(newDB.exists()){
        FileUtils.copyFile(new FileInputStream(newDB), new FileOutputStream(oldDB));
        getWritableDatabase().close();
        return true;
    }

    return false;
}

However, pressing on the import Button, the positive Toast appears, saying that the import was successfull. But my application, empty before, is still empty. The backed up database is supposed to be located on the first level of the internal storage, what it does. I think, maybe I’m confusing something with the paths, but I can’t get through what exactly.

  • 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-06-13T22:30:39+00:00Added an answer on June 13, 2026 at 10:30 pm

    Try this:

    private void importDatabase(String inputFileName) throws IOException
    {
        InputStream mInput = new FileInputStream(inputFileName);
        String outFileName = YOUR_DB_PATH_HERE;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In considering languages to use in creating a web-application that interfaces with a database
Considering that the two machine's time settings are not synced, is it still possible
I'm new to database design and I considering implementing something that will be time
I'm trying to create programmability a home shortcut for any installed application. Considering that
I'm writing a windows application that is embed with a manifest considering UAC in
Considering that I have a Schema named SBST I want to find all empty
I have a question considering a program that stimulates a stack (not using any
I'm considering using Fat Entities from Lokad.Cloud in a new Azure based application. At
This a VERY open question. Basically, I have a computing application that launches test
We have an application that using the basic JSP/servlet that all text currently in

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.