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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:42:03+00:00 2026-06-06T05:42:03+00:00

I am working on an android game. I want to copy a text file

  • 0

I am working on an android game. I want to copy a text file to external SD card when the user installs the game for the first time. The text file is important for properly running the game.

How can I do that? Where should i place the text file in eclipse source project so that when i build the apk file, my text file also gets bundled in it and when a use installs application from that apk file the text file gets copied to “SDcard\data” folder.?

What code should i write and where, so that it gets executed only once at installation time.

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-06-06T05:42:05+00:00Added an answer on June 6, 2026 at 5:42 am

    This is the methods I use to copy a file to the sd card when the app is first installed:

    public class StartUp extends Activity {
    
        /**
         * -- Called when the activity is first created.
         * ==============================================================
         **/
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FirstRun();
        }
    
        private void FirstRun() {
            SharedPreferences settings = this.getSharedPreferences("YourAppName", 0);
            boolean firstrun = settings.getBoolean("firstrun", true);
            if (firstrun) { // Checks to see if we've ran the application b4
                SharedPreferences.Editor e = settings.edit();
                e.putBoolean("firstrun", false);
                e.commit();
                // If not, run these methods:
                SetDirectory();
                Intent home = new Intent(StartUp.this, MainActivity.class);
                startActivity(home);
    
            } else { // Otherwise start the application here:
    
                Intent home = new Intent(StartUp.this, MainActivity.class);
                startActivity(home);
            }
        }
    
    /**
         * -- Check to see if the sdCard is mounted and create a directory w/in it
         * ========================================================================
         **/
        private void SetDirectory() {
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
    
                extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    
                File txtDirectory = new File(extStorageDirectory + "/yourAppName/txt/");
                // Create
                // a
                // File
                // object
                // for
                // the
                // parent
                // directory
                txtDirectory.mkdirs();// Have the object build the directory
                // structure, if needed.
                CopyAssets(); // Then run the method to copy the file.
    
            } else if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
    
                AlertsAndDialogs.sdCardMissing(this);//Or use your own method ie: Toast
            }
    
        }
    
        /**
         * -- Copy the file from the assets folder to the sdCard
         * ===========================================================
         **/
        private void CopyAssets() {
            AssetManager assetManager = getAssets();
            String[] files = null;
            try {
                files = assetManager.list("");
            } catch (IOException e) {
                Log.e("tag", e.getMessage());
            }
            for (int i = 0; i < files.length; i++) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = assetManager.open(files[i]);
                    out = new FileOutputStream(extStorageDirectory + "/yourAppName/txt/" + files[i]);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
        }
    
        private void copyFile(InputStream in, OutputStream out) throws IOException {
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on android project where I want to have a text
I'm writing my first Android game, and though the game itself is working well,
I'm working on an Android game using these tutorials over at Against the Grain
I'm working on an android game with OpenGL, and I have a game update
I'm working on a memory game for Android and I'm having a problem. When
I am developing a game for android an i'm working on the menu screen
I'm new to android game development. I currently working on a simple android app
I am working on a cross-platform mobile game for Android and iOS devices. I
Ahoy, I'm working on an OpenGL ES based game for Android using the NDK.
I am working on a game for android right now. It is a game

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.