简体   繁体   English

将目录和文件从res / raw文件夹复制到SD卡 - android

[英]Copying directories and files from res/raw folder to sd card - android

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. 我有一个文件夹,其中包含几个文件和一些目录,我需要在我第一次启动应用程序时复制到我的SD卡的/ mnt / sdcard / Android / data / path,当然,如果还没有所需的文件夹在那条路上不存在。

I will have this folder inside res/raw folder of my application. 我将把这个文件夹放在我的应用程序的res / raw文件夹中。

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. 我需要做的一步一步的程序是什么,我可以将文件夹及其所有内容从res / raw复制到SD卡中的指定路径。

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);
    }

}
 }

I suggest you the keep your files in assets. 我建议你将文件保存在资产中。 The following code may help you to copy contents from assets directory to SD Card. 以下代码可以帮助您将资产目录中的内容复制到SD卡。

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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM