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

Java 将目录和文件从res/raw文件夹复制到sd卡-android,java,android,android-intent,android-widget,android-sdk-2.1,Java,Android,Android Intent,Android Widget,Android Sdk 2.1,我有一个文件夹,其中包含一些文件和一些目录,当我第一次启动应用程序时,我需要将这些文件和目录复制到SD卡的/mnt/sdcard/Android/data/path,当然,如果还没有,那么所需的文件夹就不在该路径中 我将把这个文件夹放在我的应用程序的res/raw文件夹中 为了将文件夹及其所有内容从res/raw复制到SD卡中的指定路径,我需要执行哪些分步操作 非常感谢您的帮助 编辑 以下是帮助其他人的解决方案: @Override public void onCreate(Bundle sav

我有一个文件夹,其中包含一些文件和一些目录,当我第一次启动应用程序时,我需要将这些文件和目录复制到SD卡的/mnt/sdcard/Android/data/path,当然,如果还没有,那么所需的文件夹就不在该路径中

我将把这个文件夹放在我的应用程序的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);
    }

}
 }
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyFileOrder(“edu1”);//资源中的目录名
}
文件sdCard=Environment.getExternalStorageDirectory();
专用void copyFileOrder(字符串路径){
AssetManager AssetManager=this.getAssets();
字符串资产[]=null;
试一试{
assets=assetManager.list(路径);
如果(assets.length==0){
复制文件(路径);
}否则{
File dir=新文件(sdCard.getAbsolutePath()+“/”+“Android/data”);
//String fullPath=“/data/data/”+this.getPackageName()+“/”+path;//用于在内部存储数据/数据的路径
//文件目录=新文件(完整路径);
如果(!dir.exists()){
System.out.println(“创建目录”+sdCard.getAbsolutePath()+“/Android/data”);
布尔结果=dir.mkdir();
System.out.println(“目录创建结果”+结果);
}
对于(int i=0;i
我建议您将文件保存在资产中。以下代码可以帮助您将内容从资产目录复制到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);
            }
        }
    }
}

你介意在你目前所做的事情上发布一些代码吗?看起来你只是在说“这是我想做的,现在为我编码。”不是真的,我正在编辑我的问题。正如我在答题中明确提到的,我需要复制的不仅仅是文件,还有文件夹和子文件夹。如果你也需要从资产复制子文件夹,此链接将帮助你从原始资源复制资源,你需要修改一下副本。好的,我正在将我用作编辑的解决方案粘贴到我的问题中,以防将来对某人有所帮助。