-1

I'm trying to create a simple image file on Android and have the two following methods: Creating the directory:

    private void createThumbnailDir() {
    File file = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + File.separator + "scouthouse",
            "scouthouse_thumbnails");
    this.getActivity().sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                    + Environment.getExternalStorageDirectory())));
    if (!file.mkdirs()) {
        Log.d("file", "file not created");
    }
}

Creating the file:

    private File createNewThumbnailFile() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss",
            Locale.ENGLISH);
    Date date = Calendar.getInstance().getTime();
    File file = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "scouthouse" + File.separator + "schouthouse_thumbnails" + File.separator
                    + sdf.format(date) + ".jpg");
    try {
        if (file.createNewFile()) {
            return file;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

When I create the file the following IOException is raised: java.io.IOException: ENOENT (No such file or directory)

But the directory does exist when I check the file manager on my phone.

Edit:

More about the error: stacktrace = null, so I only have the cause and the detailmessage, and they're both the same: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)

Tim Kranen
  • 4,202
  • 4
  • 26
  • 49

2 Answers2

3

Do you have this on your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

? EDIT:

      private File createThumbnailDir() {
        File file = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        + File.separator + "scouthouse",
                "scouthouse_thumbnails");
        this.getActivity().sendBroadcast(
                new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));
        if (!file.mkdirs()) {
            Log.d("file", "file not created");
            return file;
        }else {return null}
    }  

Now:

  private File createNewThumbnailFile() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss",
            Locale.ENGLISH);
    Date date = Calendar.getInstance().getTime();
    String filename= sdf.format(date) + ".jpg";
    File file = new File(createThumbnailDir(), filename); 

 try {
    outStream = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPG, 100, outStream);
    outStream.flush();
    outStream.close();

    Toast.makeText(AndroidWebImage.this, "Saved", Toast.LENGTH_LONG).show();

   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   }

    return null;
}

reference

Dyna
  • 2,304
  • 1
  • 14
  • 25
  • I should've noted that in the story, yes I have the required permissions. – Tim Kranen Sep 06 '13 at 14:11
  • what is the line of the error? maybe you should put your logcat or something to complete your question. – Dyna Sep 06 '13 at 14:14
  • If you already created the file in the first method why dont you use it to save the image? I will update my answer with that – Dyna Sep 06 '13 at 14:30
0

Have you tried saving your file somewhere else? Your app might have no write permission.

nightsnaker
  • 471
  • 6
  • 16