11

when i was inserting the bitmap image to files directory, it showing file not found exception and it is showing Is a Directory.

Here is my code:

            File mFolder = new File(getFilesDir() + "/sample");

            if (!mFolder.exists()) {
                mFolder.mkdir();
            }
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(mFolder);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

                 e.printStackTrace();
             } catch (Exception e) {

                 e.printStackTrace();
             }

Logcat:

07-12 01:08:05.434: W/System.err(8170): java.io.FileNotFoundException: /data/data/com.sample.sam/files/sample(Is a directory)
07-12 01:08:05.434: W/System.err(8170):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
07-12 01:08:05.434: W/System.err(8170):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
Stephen
  • 9,899
  • 16
  • 90
  • 137
srinu_stack
  • 117
  • 1
  • 2
  • 7

4 Answers4

20

you have to create file, before writing into stream.

File mFolder = new File(getFilesDir() + "/sample");
File imgFile = new File(mFolder.getAbsolutePath() + "/someimage.png");
if (!mFolder.exists()) {
    mFolder.mkdir();
}
if (!imgFile.exists()) {
    imgFile.createNewFile();
}
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(imgFile);
    bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
    fos.flush();
    fos.close();
} catch (IOException e) {
     e.printStackTrace();
}
Autocrab
  • 3,474
  • 1
  • 15
  • 15
1

Use file.createNewFile() if you want to make a file and not a directory. You may also need to use mkDirs() if the path doesn't exist either.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
0

You are opening the directory itself for writing. This does not work. You need to specify a file within the directory, e.g.:

fos = new FileOutputStream(new File(mFolder, "myfile"));
Henry
  • 42,982
  • 7
  • 68
  • 84
-1
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/text/");
dir.mkdir();
File file = new File(dir, "sample.txt");
FileOutputStream os = null;
try {
    os = new FileOutputStream(file);
    os.write(enterText.getText().toString().getBytes());
    os.close();
}
catch (IOException e) {
    e.printStackTrace();
}
gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
Sujit Jamdade
  • 211
  • 2
  • 4
  • Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jan 14 '20 at 08:29