-1

我想在 sd 卡中创建一个文件并将我的图像从资源中放入其中。但问题是我在这一行中遇到异常:

    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)

请指教我哪里错了???并给我解决方案

4

1 回答 1

0

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

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

于 2012-08-25T18:07:03.120 回答