获取filenotfound异常android写入sd卡

时间:2012-08-25 18:00:39

标签: android sd-card filenotfoundexception

我想在SD卡中创建一个文件并将我的图片从resouces放入其中。但问题是我在这行中得到了一个例外:

    OutputStream out = new FileOutputStream(newFile);

这是我的方法是调用按钮点击和日志猫

    private void openSaveDialog(){try {
             String dirName = "/sdcard/ABCD"; 

            File newFile = new File(dirName); 
            newFile.mkdir(); 
            String str ="android.resource://com.sample.projectnew/"+ID ;

            // convert String into InputStream
            InputStream in = new ByteArrayInputStream(str.getBytes());

            OutputStream out = new FileOutputStream(newFile);


                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                    Toast.makeText(GActivity.this,"Your image is saved to this folder",Toast.LENGTH_LONG)
                            .show();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Toast.makeText(GActivity.this,
                    "Your got exception", Toast.LENGTH_LONG)
                    .show();
            e.printStackTrace();
        } }

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

日志猫在这里:

     08-25 23:00:57.949: W/System.err(9190): **java.io.FileNotFoundException: /sdcard/ABCD (Is a directory)**
08-25 23:00:57.949: W/System.err(9190):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-25 23:00:57.949: W/System.err(9190):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-25 23:00:57.949: W/System.err(9190):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
08-25 23:00:57.949: W/System.err(9190):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)

请告诉我哪里错了???并给我解决方案

1 个答案:

答案 0 :(得分:0)

您正在尝试写入目录。尝试这样的事情:

OutputStream out = new FileOutputStream(new File(newFile, "filename"));

相关问题