如何将文件从“assets”文件夹复制到SD卡?

269

我有一些文件在assets文件夹中。我需要将它们全部复制到一个名为/sdcard/folder的文件夹中。我想要在一个线程内完成此操作。如何做到这一点?


你正在寻找这个吗?https://dev59.com/7m855IYBdhLWcg3wQx5L#25988337 - DropAndTrap
2
在你复制/粘贴下面(很棒!)的解决方案之前,考虑使用这个库在一行代码中完成它:https://dev59.com/7m855IYBdhLWcg3wQx5L#41970539 - JohnnyLambada
23个回答

0

你也可以使用Guava的ByteStream将文件从资产文件夹复制到SD卡。这是我最终采用的解决方案,它可以递归地从资产文件夹复制文件到SD卡:

/**
 * Copies all assets in an assets directory to the SD file system.
 */
public class CopyAssetsToSDHelper {

    public static void copyAssets(String assetDir, String targetDir, Context context) 
        throws IOException {
        AssetManager assets = context.getAssets();
        String[] list = assets.list(assetDir);
        for (String f : Objects.requireNonNull(list)) {
            if (f.indexOf(".") > 1) { // check, if this is a file
                File outFile = new File(context.getExternalFilesDir(null), 
                    String.format("%s/%s", targetDir, f));
                File parentFile = outFile.getParentFile();
                if (!Objects.requireNonNull(parentFile).exists()) {
                    if (!parentFile.mkdirs()) {
                        throw new IOException(String.format("Could not create directory %s.", 
                            parentFile));
                    }
                }
                try (InputStream fin = assets.open(String.format("%s/%s", assetDir, f));
                     OutputStream fout = new FileOutputStream(outFile)) {
                    ByteStreams.copy(fin, fout);
                }
            } else { // This is a directory
                copyAssets(String.format("%s/%s", assetDir, f), String.format("%s/%s", targetDir, f), 
                    context);
            }
        }
    }

}

0

这绝对是我在互联网上找到的最好的解决方案。 我使用了以下链接 https://gist.github.com/mhasby/026f02b33fcc4207b302a60645f6e217
但它有一个错误,我修复了它,现在它像魔法一样运行。 这是我的代码。你可以很容易地使用它,因为它是一个独立的Java类。

public class CopyAssets {
public static void copyAssets(Context context) {
    AssetManager assetManager = context.getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (files != null) for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);

            out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/www/resources/" + filename);
            copyFile(in, out);
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {

                }
            }
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                    out = null;
                } catch (IOException e) {

                }
            }
        }
    }
}

public static 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);
    }
}}

正如你所看到的,在具有活动的Java类中创建CopyAssets的实例即可。现在这一部分非常重要,根据我的测试和互联网研究,如果类没有活动,您不能使用AssetManager 。 这与Java类的上下文有关。
现在,c.copyAssets(getApplicationContext())是访问该方法的简单方法,其中cCopyAssets类的实例。 根据我的要求,我允许程序将所有资源文件复制到/www/resources/的内部目录中的asset文件夹中。
您可以轻松找到需要根据您的用途更改目录的部分。 如果您需要任何帮助,请随时联系我。


-1
使用AssetManager,它允许读取assets中的文件。然后使用常规的Java IO将文件写入sdcard。
谷歌是你的朋友,搜索一个例子。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接