Android的:如何在SD卡中创建一个目录,并从/ RES文件拷贝/生呢? [英] Android: How to create a directory on the SD Card and copy files from /res/raw to it?

查看:136
本文介绍了Android的:如何在SD卡中创建一个目录,并从/ RES文件拷贝/生呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建对SD卡的文件夹和几个子目录内它...然后我想,我已经保存在/ RES文件/原料转移到该文件夹​​......我另外,我想这只发生一次,在第一时间的节目的要求不断运行。我知道,这是可笑的开放式的,而我问了很多......但任何帮助将是很大的AP preciated。

I am trying to create a folder and several subdirectory within it on the SD Card... I then want to transfer files that I have stored in /res/raw to that folder... I addition, I want this to only happen once, the first time the program is ever run. I realize that this is ridiculously open-ended, and that I am asking a lot... but any help would be greatly appreciated.

推荐答案

这将在apk文件资产文件夹的剪贴画子文件夹中的所有文件复制到SD卡上的应用程序的文件夹中的剪贴画子文件夹:

This will copy all files in the "clipart" subfolder of the .apk assets folder to the "clipart" subfolder of your app's folder on the SD card:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    String basepath = extStorageDirectory + "/name of your app folder on the SD card";
//...

// in onCreate
File clipartdir = new File(basepath + "/clipart/");
        if (!clipartdir.exists()) {
            clipartdir.mkdirs();
            copyClipart();      
        }

private void copyClipart() {
        AssetManager assetManager = getResources().getAssets();
        String[] files = null;
        try {
            files = assetManager.list("clipart");
        } catch (Exception e) {
            Log.e("read clipart ERROR", e.toString());
            e.printStackTrace();
        }
        for(int i=0; i<files.length; i++) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open("clipart/" + files[i]);
              out = new FileOutputStream(basepath + "/clipart/" + files[i]);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
            } catch(Exception e) {
                Log.e("copy clipart ERROR", e.toString());
                e.printStackTrace();
            }       
        }
    }
    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
          out.write(buffer, 0, read);
        }
    }

这篇关于Android的:如何在SD卡中创建一个目录,并从/ RES文件拷贝/生呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆