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

6
我有一个文件夹,里面包含一些文件和目录。当我第一次启动应用程序时,我需要将这个文件夹及其所有内容复制到SD卡的/mnt/sdcard/Android/data/路径下,如果该路径中不存在所需的文件夹。
我将把这个文件夹放在我的应用程序的res/raw文件夹中。
请问,我需要按照哪些步骤操作才能将res/raw中的文件夹及其所有内容从复制到SD卡上指定的路径?
非常感谢您的帮助。
编辑:
以下是解决方案,如果对其他人有帮助:
@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);
    }

}
 }

你介意贴一些你已经完成的代码吗?看起来你只是在说“这是我想做的,现在为我编码。” - Jason L
不是真的,我正在编辑我的问题。 - user1400538
1个回答

3

我建议你将文件放在assets目录中。以下代码可以帮助你将内容从assets目录复制到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);
            }
        }
    }
}

正如我在问题中明确提到的那样,我需要复制的不仅仅是文件,还有文件夹和子文件夹。 - user1400538
这个链接将帮助您从原始资源中复制资源 https://dev59.com/ynNA5IYBdhLWcg3wfd8m - vineet
如果您需要从资产中复制子文件夹,您需要稍微修改一下复制操作。 - vineet
好的,我将我的解决方案作为编辑粘贴到了我的问题中,以防未来有人需要。 - user1400538

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接