【问题标题】:android copy file from assets to sd cardandroid将文件从资产复制到SD卡
【发布时间】:2014-11-23 23:16:55
【问题描述】:

这是 MyNewMain.java

        CopyAssets();


    private void CopyAssets() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try {
            files = assetManager.list("Files");
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        for(String filename : files) {
            System.out.println("File name => "+filename);
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open("Files/"+filename);
                out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
            }
        }
    }
    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);
        }
    }

在我的资产文件夹中,我有一个名为 Files 的文件夹。有.txt文件 onCreate 方法我正在调用 CopyAssets();

下面是我使用的方法。

问题是这什么也没做。我绝对不知道为什么我的文件没有被复制。 在我的清单中我添加了

并且 app.iml 包含

【问题讨论】:

  • 什么都没有? System.out.println("File name => "+filename);。那打印什么?你为什么要发布这么多与你的问题无关的代码?只有 CopyAssets() 会这样做。请编辑您的帖子。
  • Environment.getExternalStorageDirectory().toString()。试试Environment.getExternalStorageDirectory().getAbsolutePath()
  • 我已经编辑了我的帖子。
  • 我已经尝试过 greenapps 建议的 .getAbsolutePath() 但没有用
  • 这段代码很完美..检查你是否在manifest.xml中设置了所需的权限

标签: java android methods copy assets


【解决方案1】:

我知道这个问题已经得到解答,但我有一种更优雅的方式可以从资产目录复制到 sdcard 上的文件。它不需要“for”循环,而是使用文件流和通道来完成工作。

(注意)如果使用任何类型的压缩文件,APK,PDF,...您可能需要在插入资产之前重命名文件扩展名,然后在将其复制到 SD 卡后重命名)

AssetManager am = context.getAssets();
AssetFileDescriptor afd = null;
try {
    afd = am.openFd( "MyFile.dat");

    // Create new file to copy into.
    File file = new File(Environment.getExternalStorageDirectory() + java.io.File.separator + "NewFile.dat");
    file.createNewFile();

    copyFdToFile(afd.getFileDescriptor(), file);

} catch (IOException e) {
    e.printStackTrace();
}

一种无需循环即可复制文件的方法。

public static void copyFdToFile(FileDescriptor src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

感谢JPM's Answer

【讨论】:

    【解决方案2】:

    在 android api 22 上,在 AssetFileDescriptor 对象上调用 getFileDescriptor() 将返回整个 apk 文件的 FileDescriptor。所以@Salmaan 的答案在 android api 22 上是错误的。

    我没有在其他 api 中看到源代码。所以我不知道getFileDescriptor()在其他api中的行为。

    我在this question下找到答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多