从资产到本地目录复制目录

从资产到本地目录复制目录

本文介绍了从资产到本地目录复制目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用,我在我的资产文件夹和访问它作为一个文件的目录。是否有可能访问的东西,在资产目录作为文件?如果没有,我怎样才能从资产的文件夹目录复制到应用程序的本地目录?

我会复制文件,如下所示:

 尝试
    {
        InputStream的流= this.getAssets()开(MYFILE)。
        的OutputStream输出=新的BufferedOutputStream(新的FileOutputStream(this.getFilesDir()+/ myNewFile));

        字节的数据[] =新的字节[1024];
        诠释计数;

        而((计数= stream.read(数据))!=  -  1)
        {
            output.write(数据,0,计数);
        }

        output.flush();
        output.close();
        stream.close();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
 

不过,我不知道我会怎样能够做到这一点的一个目录。

我宁愿不建立自己身边的东西,不工作的基础,所以我怎么会从资产中复制一个目录到本地目录,或是否有可能访问我的资产目录作为文件

修改

这是我如何解决它为我自己的项目:

 的InputStream流= NULL;
OutputStream的输出= NULL;

对于(字符串文件名:this.getAssets()列表(demopass))
{
    流= this.getAssets()开(目录名/+文件名)。
    输出=新的BufferedOutputStream(新的FileOutputStream(this.getFilesDir()+/ newDirectory /+文件名));

    字节的数据[] =新的字节[1024];
    诠释计数;

    而((计数= stream.read(数据))!=  -  1)
    {
        output.write(数据,0,计数);
    }

    output.flush();
    output.close();
    stream.close();

    流= NULL;
    输出= NULL;
}
 

解决方案

由于上述意见建议的dmaxi,你可以用自己的链接,与此code:

 无效displayFiles(AssetManager经理,字符串路径){
        尝试 {
            字符串列表[] = mgr.list(路径);
            如果(名单!= NULL)
                的for(int i = 0; I< list.length ++ I)
                {
                    Log.v(资产,路径+/+列表[I]);
                    displayFiles(MGR,路径+/+列表[I]);
                }
        }赶上(IOException异常E){
             Log.v(列出的错误:,不能列出+路径);
        }
     }
 

我把它放在

 私人无效copyFolder(字符串名称){
            //名是你的文件夹的名称!
    AssetManager assetManager = getAssets();
    的String []文件= NULL;

    字符串状态= Environment.getExternalStorageState();

    如果(Environment.MEDIA_MOUNTED.equals(州)){
        //我们可以读取和写入的媒体
        //检查资产子文件夹中的文件
        尝试 {
            文件= assetManager.list(名称);
        }赶上(IOException异常E){
            Log.e(错误,无法获取资源文件列表。,E);
        }
        //分析资产子文件夹中的所有文件
        对于(字符串文件名:文件){
            在的InputStream = NULL;
            出的OutputStream = NULL;
            //第一:检查是否已经有一个目标文件夹
            文件夹=新的文件(Environment.getExternalStorageDirectory()+/ yourTargetFolder /+姓名);
            布尔成功= TRUE;
            如果(!folder.exists()){
                成功= folder.mkdir();
            }
            如果(成功){
                //移动的外置SD所有文件
                尝试 {
                    在= assetManager.open(名称+/+文件名);
                    OUT =新的FileOutputStream(Environment.getExternalStorageDirectory()+/ yourTargetFolder /+姓名+/+文件名);
                    Log.i(web视图,Environment.getExternalStorageDirectory()+/ yourTargetFolder /+名称+/+文件名);
                    的CopyFile(IN,OUT);
                    附寄();
                    在= NULL;
                    了out.flush();
                    out.close();
                    OUT = NULL;
                }赶上(IOException异常E){
                    Log.e(错误,无法复制资源文件:+文件名,E);
                } 最后 {
                    //编辑3(MMS后评论)
                    附寄();
                    在= NULL;
                    了out.flush();
                    out.close();
                    OUT = NULL;
                }
            }
            其他 {
                //做别的事情上失败
            }
        }
    }否则,如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
        //我们只能读取介质
    } 其他 {
        //别的东西是错误的。这可能是很多其他的国家之一,但我们需要
        //是知道的是,我们既不能读也不能写
    }
}

//使用copyAssets()故意复制文件的方法。
私人无效的CopyFile(在的InputStream,OutputStream的了)抛出IOException异常{
    byte []的缓冲区=新的字节[1024];
    INT读取;
    而((读= in.read(缓冲))!= -1){
        out.write(缓冲,0,读);
    }
}
 

编辑2: i'have在上面添加一个例子:这块code复制从资产只有一个特定的文件夹,到SD卡。让我知道,如果它的工作原理!

I'm trying to use a directory that I have in my assets folder and access it as a File. Is it possible to access something in the Assets directory as a File? If not, how can I copy a directory from the Assets folder to the application's local directory?

I would copy a file like so:

    try
    {
        InputStream stream = this.getAssets().open("myFile");
        OutputStream output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/myNewFile"));

        byte data[] = new byte[1024];
        int count;

        while((count = stream.read(data)) != -1)
        {
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        stream.close();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

However, I'm not sure how I would be able to do this for a directory.

I would rather not build my infrastructure around something that doesn't work, so how would I copy a directory from Assets to a local directory, or is it possible to access a directory in my Assets as a File?

EDIT

This is how I solved it for my own project:

InputStream stream = null;
OutputStream output = null;

for(String fileName : this.getAssets().list("demopass"))
{
    stream = this.getAssets().open("directoryName/" + fileName);
    output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/newDirectory/" + fileName));

    byte data[] = new byte[1024];
    int count;

    while((count = stream.read(data)) != -1)
    {
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    stream.close();

    stream = null;
    output = null;
}
解决方案

As suggested by dmaxi in comment above, you can use his link, with this code:

    void displayFiles (AssetManager mgr, String path) {
        try {
            String list[] = mgr.list(path);
            if (list != null)
                for (int i=0; i<list.length; ++i)
                {
                    Log.v("Assets:", path +"/"+ list[i]);
                    displayFiles(mgr, path + "/" + list[i]);
                }
        } catch (IOException e) {
             Log.v("List error:", "can't list" + path);
        }
     }

I took it on this link.Maybe you can combine this code with precedent one.

EDIT: see also AssetManager.

private void copyFolder(String name) {
            // "Name" is the name of your folder!
    AssetManager assetManager = getAssets();
    String[] files = null;

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        // Checking file on assets subfolder
        try {
            files = assetManager.list(name);
        } catch (IOException e) {
            Log.e("ERROR", "Failed to get asset file list.", e);
        }
        // Analyzing all file on assets subfolder
        for(String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            // First: checking if there is already a target folder
            File folder = new File(Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name);
            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }
            if (success) {
                // Moving all the files on external SD
                try {
                    in = assetManager.open(name + "/" +filename);
                    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name + "/" + filename);
                    Log.i("WEBVIEW", Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name + "/" + filename);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                } catch(IOException e) {
                    Log.e("ERROR", "Failed to copy asset file: " + filename, e);
                } finally {
                    // Edit 3 (after MMs comment)
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                }
            }
            else {
                // Do something else on failure
            }
        }
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
    } else {
        // Something else is wrong. It may be one of many other states, but all we need
        // is to know is we can neither read nor write
    }
}

// Method used by copyAssets() on purpose to copy a file.
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);
    }
}

EDIT 2: i'have added an example above: this piece of code copy only a specific folder from assets, to sd card. Let me know if it works!

这篇关于从资产到本地目录复制目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:50