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 8463743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:32:25+00:00 2026-06-10T14:32:25+00:00

I am developing an Android that uses a database. I want to copy the

  • 0

I am developing an Android that uses a database. I want to copy the database to the SD card when use true machine run. But it doesn’t always succeed. My program doesn’t say why, only that it can’t copy the database. I store the database in res\raw\mobilephone1.db; it’s 10 MB. I set the permissions to allow writing data to the SD card.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

Is this right?

private final String DATABASE_PATH = android.os.Environment  
                .getExternalStorageDirectory().getAbsolutePath()  
                + "/mobilephone";  
    private final String DATABASE_FILENAME = "mobilephone.db"; 


private SQLiteDatabase openDatabase()  
    {  
        try  
        {  
            // obtain absolute path of mobilephone.db  
            String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;  
            File dir = new File(DATABASE_PATH);          
            // if/sdcard/mobilephone exist,crtate this list 
            if (!dir.exists()) {  
                dir.mkdir();  
                }
            // if  mobilephone.db file did not exist in/sdcard/mobilephone then copy this filt to SD card list(/sdcard/mobilephone) from res\raw
            if (!(new File(databaseFilename)).exists())  
            {  

                // obtain the InputStream object that encapsulate mobilephone.db
                InputStream is = getResources().openRawResource(R.raw.mobilephone1);  
                FileOutputStream fos = new FileOutputStream(databaseFilename);  

                int count = 0;
                while (count == 0) {
                 count = is.available();
                }
                byte[] buffer = new byte[count];

                int readCount = 0; // the number of bytes that has successfully readen
                while (readCount < count) {
                 readCount += is.read(buffer, readCount, count - readCount);
                }

                fos.write(buffer, 0, count); 
                fos.close();  
                is.close();  
            }  
            // go to mobilephone.db in /sdcard/mobilephone  
            SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(  
                   databaseFilename, null);  
            return database;  
        }  
        catch (Exception e)  
        {  
        }  
        return null;  
    }  
  • 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-10T14:32:27+00:00Added an answer on June 10, 2026 at 2:32 pm

    Try this one out ::

    File sd = Environment.getExternalStorageDirectory();
    
    File src = new File("YOUR_SOURCE_PATH");
    
    File dest = new File(sd +  "/" +DATABASE_NAME);
    
          try 
          {
              if (Environment.getExternalStorageDirectory().canWrite()) 
              {                 
                  if (srcdb.exists()) 
                  {
                      FileChannel src = new FileInputStream(srcdb).getChannel();
                      FileChannel dst = new FileOutputStream(destdb).getChannel();
                      dst.transferFrom(src, 0, src.size());
                      src.close();
                      dst.close();  
                      flag=0; 
    
                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(), "ERROR: File not found in Database " , Toast.LENGTH_LONG).show();                    
                  }
              }
              else
              {
                  Toast.makeText(getApplicationContext(), "ERROR: Cannot write to file" , Toast.LENGTH_LONG).show();
              }
          }
          catch (Exception e) 
          {
              Log.e("Movedb", "Error in Copying" + e.toString());
    
          }
    

    Also you need only one permission :

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an Android application that uses a MapView . I'd like to run
I am developing an android application which uses the SQLite database. In that case,
I'm developing an Android application that uses the camera and will run on Android
I'm developing an Android app that uses Osmdroid to display the maps in offline
I'm currently developing an android application that uses sockets to connect to a local
The scenario is, i'm developing an android app that uses the internet. This is
I'm developing an augmented reality application for Android that uses the phone's camera to
I'm developing an Android 3.1 application that uses USB Host mode to communicate with
Good evening. I'm developing an android application that uses Facebook SDK. I've no problem
I'm developing for Android and I have an app (called Forget-Me-Not) that uses a

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.