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

标签 java android android-intent android-widget android-sdk-2.1

我有一个文件夹,其中包含一些文件和一些目录,我需要在第一次启动应用程序时将其复制到我的 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);
    }

}
 }

最佳答案

我建议您将文件保存在 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);
            }
        }
    }
}

关于java - 将目录和文件从 res/raw 文件夹复制到 SD 卡 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11084393/

相关文章:

Java 正则表达式模式未按预期工作

c# - 像java一样的Visual Studio参数化单元测试

java - 将 Spring Boot 示例 JNDI 转换为在单独的 Tomcat 实例中工作

Java 堆栈与队列性能

android - 如何打开图库以选择多个图像?

Android SMS Intent Hangouts 2.0

android - 让 picasso 与 "if-modified-since"和 "ETag"等 header 配合使用

android - Android Gmail 收件箱屏幕上的添加按钮

java - 在 TextView 中使用不同的字体样式

java - Android:OnClick 按钮不会向 TextView 添加/删除值