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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:38:32+00:00 2026-06-06T04:38:32+00:00

I have a folder which contains few files and some directories which I need

  • 0

I have a folder which contains few files and some directories which I need to copy to my SD card’s /mnt/sdcard/Android/data/ path while I launch the application for the first time, and of course, if not already the required folder is not present in that path.

I will have this folder inside res/raw folder of my application.

What are the step by step procedures I need to do such that I can copy the folder and all its contents from res/raw to the specified path in the SD card.

Any help is much appreciated.

Edit

The following is the solution if it helps someone else:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyFileOrDir("edu1");//directory name in assets
}
File sdCard = Environment.getExternalStorageDirectory();
private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            File dir = new File (sdCard.getAbsolutePath() + "/" + "Android/data");
            //String fullPath = "/data/data/" + this.getPackageName() + "/" + path;//path for storing internally to data/data
            //File dir = new File(fullPath);
            if (!dir.exists()){
                System.out.println("Created directory"+sdCard.getAbsolutePath() + "/Android/data");
                boolean result = dir.mkdir();
                System.out.println("Result of directory creation"+result);
            }

            for (int i = 0; i < assets.length; ++i) {
                copyFileOrDir(path + "/" + assets[i]);
            }
        }
    } catch (IOException ex) {
        System.out.println("Exception in copyFileOrDir"+ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        //String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;//path for storing internally to data/data
        String newFileName = sdCard.getAbsolutePath() + "/Android/data/" + filename;
        out = new FileOutputStream(newFileName);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        System.out.println("Exception in copyFile"+e);
    }

}
 }
  • 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-06T04:38:34+00:00Added an answer on June 6, 2026 at 4:38 am

    I suggest you the keep your files in assets. The following code may help you to copy contents from assets directory to SD Card.

    public static void copyFile(Activity c, String filename) 
    {
        AssetManager assetManager = c.getAssets();
    
        InputStream in = null;
        OutputStream out = null;
        try 
        {
            in = assetManager.open(filename);
            String newFileName = sdcardpath/filename;
            out = new FileOutputStream(newFileName);
    
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) 
            {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Utility.printLog("tag", e.getMessage());
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    printLog(TAG, "Exception while closing input stream",e);
                }
            }
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    printLog(TAG, "Exception while closing output stream",e);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder A which contains some Python files and __init__.py. If I
I have a bunch of .doc files in a folder which I need to
I have a folder which contains a ~50 text files (PHP) and hundreds of
I have a folder which contains the following files: Elephant.19864.archive.other.pdf Elephant.17334.other.something.pdf Turnip.19864.something.knight.pdf Camera.22378.nothing.elf.pdf I
I have subversioned my entire folder which contains all the source files, binaries and
I have a image folder which contains sub directory for each album of images
I use special characters (swedish letters åäö). Now, I have some folders, which contains
I have a content folder which is full of other sub-directories named in the
I have a folder C:\Images which has a some images. This folder is not
I have a folder called Client which contains many subfolders. I want to create

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.