将数据复制到SD卡时的FileNotFoundException - Android

时间:2012-06-19 13:09:45

标签: java android android-intent android-widget

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

行中找到File not found异常

当我将其保存到/ data / data文件夹时,我得到了完美的工作;即;内部存储器。我检查了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

有人可以让我知道是什么导致这种情况,因为我找不到任何明显的原因吗?非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

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

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的创建将失败。

答案 1 :(得分:1)

以这种方式试试.......

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

FileWriter fw = new FileWriter(f);

BufferedWriter bw = new BufferedWriter(fw);

答案 2 :(得分:1)

在你的代码中你检查sdcard路径是否存在,同时你应该检查你的路径导致dir“edu1”永远不会被创建尝试使用它而不是

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