【问题标题】:Copying directories and files from res/raw folder to sd card - android将目录和文件从 res/raw 文件夹复制到 sd 卡 - android
【发布时间】:2012-06-20 12:20:12
【问题描述】:

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

}
 }

【问题讨论】:

  • 您介意发布一些关于您迄今为止所做的代码吗?看起来您只是在说“这是我想做的,现在为我编写代码。”
  • 不是真的,我正在编辑我的问题。

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


【解决方案1】:

我建议您将文件保存在资产中。以下代码可以帮助您将资产目录中的内容复制到 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);
            }
        }
    }
}

【讨论】:

  • 正如我在 qstion 中明确提到的,我需要复制的不仅仅是文件,还包括文件夹和子文件夹
  • 此链接将帮助您从原始资源stackoverflow.com/questions/939170/…复制资源
  • 如果您也需要从 assets 中复制子文件夹,您需要稍微修改一下副本。
  • 好的,我将我用作编辑的解决方案粘贴到我的问题中,以防将来对某人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
相关资源
最近更新 更多