【问题标题】:FileNotFoundException while copying data to SD Card - Android将数据复制到 SD 卡时出现 FileNotFoundException - Android
【发布时间】:2012-06-19 13:09:44
【问题描述】:

以下是我用来复制包含 txt 文件的文件夹的代码。该文件夹位于我的应用程序的资产文件夹中。当我复制时,我在 out = new FileOutputStream(newFileName);

行中得到 File not found 异常

当我将它保存到 /data/data 文件夹时,我可以完美地工作; IE;内部存储器。我检查了 SD 卡的状态,它显示已安装。

public class CpyAsset extends Activity {
/** Called when the activity is first created. */
@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());
            if (!dir.exists()){
                System.out.println("Created directory"+sdCard.getAbsolutePath());
                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 = sdCard.getAbsolutePath() + "/"+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);
    }

}
}

例外

01-01 06:13:34.783: INFO/System.out(11334): Exception in copyFilejava.io.FileNotFoundException: /mnt/sdcard/edu1/anees.txt: open failed: ENOENT (No such file or directory)

我尝试复制的文件夹(和内容)在 assets/edu1/abc.txt 中

有人可以告诉我是什么原因造成的,因为我找不到任何明显的原因吗?非常感谢任何帮助。

【问题讨论】:

  • 您是否在清单中设置了权限 WRITE_EXTERNAL_STORAGE?
  • 是的,我已经设置了权限` `
  • 不应该是 if (!(assets.length == 0)) { ?
  • 将 e.printstacktrace 添加到您的异常处理程序中,以便它停止隐藏错误详细信息。还要添加 copyFile 尝试的文件的日志记录,并发布整个日志。

标签: java android android-intent android-widget


【解决方案1】:

您总是在这部分尝试创建外部存储根目录:

File dir = new File (sdCard.getAbsolutePath());
if (!dir.exists()){
            System.out.println("Created directory"+sdCard.getAbsolutePath());
            boolean result = dir.mkdir();
            System.out.println("Result of directory creation"+result);
}

所以您没有创建文件夹 edu1/,在该文件夹中创建文件 anees.txt 将失败。

【讨论】:

  • 按照您和 Amal 的建议尝试过,现在不存在异常。但我在 /mnt/sdcard/edu1/anees.txt 中看不到文件
【解决方案2】:

在您的代码中,您检查 sdcard 路径是否存在,而您应该检查导致目录“edu1”从未创建的路径尝试改用它

File dir = new File (sdCard.getAbsolutePath()+"/"+path);

【讨论】:

  • 现在异常消失了,但我在路径路径 /mnt/sdcard/edu1/anees.txt 中没有看到该文件。为什么?
  • @user1400538 你应该看到了我已经尝试了修改后的代码并找到了文件,它应该可以正常工作
【解决方案3】:

试试这个方法......

File f = new File("/sdcard/assets/edu1/abc.txt");

FileWriter fw = new FileWriter(f);

BufferedWriter bw = new BufferedWriter(fw);

【讨论】:

  • 它将在 sdcard 的 assets/edu1 位置创建一个文件名 abc.txt,并将通过从源获取输入来写入...现在源可以是控制台、文件...等。 ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多