-1

I want to create a file in sd card and put my image from resouces into it.But the problem is I get a exception in this line:

    OutputStream out = new FileOutputStream(newFile);

here is my method am calling on button click and the log cat

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

the log cat is here:

     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)

Please advice me where I am wrong??? and give me the solution

Ali
  • 9,800
  • 19
  • 72
  • 152
user1125690
  • 137
  • 1
  • 1
  • 8

1 Answers1

0

You are trying to write to a directory. Try something like so:

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

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Thanks anyway! But I got my answer from this link: http://stackoverflow.com/questions/4263375/android-saving-created-bitmap-to-directory-on-sd-card – user1125690 Aug 25 '12 at 18:53